Skip to content

Commit fd73e94

Browse files
committed
Document all the functions (mostly). Also, ensure a few functions do not alter in place.
1 parent c1a18fd commit fd73e94

File tree

7 files changed

+270
-18
lines changed

7 files changed

+270
-18
lines changed

LICENSE

+6-6
Original file line numberDiff line numberDiff line change
@@ -292,21 +292,21 @@ Definitions:
292292

293293
- "Package" refers to the collection of files distributed by the Copyright
294294
Holder, and derivatives of that collection of files created through
295-
textual modification.
295+
textual modification.
296296
- "Standard Version" refers to such a Package if it has not been modified,
297297
or has been modified in accordance with the wishes of the Copyright
298-
Holder.
298+
Holder.
299299
- "Copyright Holder" is whoever is named in the copyright or copyrights for
300-
the package.
300+
the package.
301301
- "You" is you, if you're thinking about copying or distributing this Package.
302302
- "Reasonable copying fee" is whatever you can justify on the basis of media
303303
cost, duplication charges, time of people involved, and so on. (You will
304304
not be required to justify it to the Copyright Holder, but only to the
305-
computing community at large as a market that must bear the fee.)
305+
computing community at large as a market that must bear the fee.)
306306
- "Freely Available" means that no fee is charged for the item itself, though
307307
there may be fees involved in handling the item. It also means that
308308
recipients of the item may redistribute it under the same conditions they
309-
received it.
309+
received it.
310310

311311
1. You may make and give away verbatim copies of the source form of the
312312
Standard Version of this Package without restriction, provided that you
@@ -373,7 +373,7 @@ products derived from this software without specific prior written permission.
373373

374374
9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
375375
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
376-
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
376+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
377377

378378
The End
379379

META.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Chase Whitener <[email protected]>"
55
],
66
"dynamic_config" : 1,
7-
"generated_by" : "Dist::Zilla version 6.014, CPAN::Meta::Converter version 2.150010",
7+
"generated_by" : "Dist::Zilla version 6.024, CPAN::Meta::Converter version 2.150010",
88
"license" : [
99
"perl_5"
1010
],
@@ -102,8 +102,8 @@
102102
"x_contributors" : [
103103
"Chase Whitener <[email protected]>"
104104
],
105-
"x_generated_by_perl" : "v5.26.1",
106-
"x_serialization_backend" : "Cpanel::JSON::XS version 4.19",
105+
"x_generated_by_perl" : "v5.30.0",
106+
"x_serialization_backend" : "Cpanel::JSON::XS version 4.27",
107107
"x_spdx_expression" : "Artistic-1.0-Perl OR GPL-1.0-or-later"
108108
}
109109

Makefile.PL

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This Makefile.PL for Sodium-FFI was generated by
2-
# Dist::Zilla::Plugin::MakeMaker::Awesome 0.48.
2+
# Dist::Zilla::Plugin::MakeMaker::Awesome 0.49.
33
# Don't edit it but the dist.ini and plugins used to construct it.
44

55
use strict;

README.md

+131
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,139 @@ Sodium::FFI - FFI implementation of libsodium
66

77
```perl
88
use strict;
9+
use warnings;
10+
use v5.34;
11+
12+
use Sodium::FFI ();
13+
14+
my $text = "1234";
15+
my $padded = Sodium::FFI::pad($text, 16);
16+
say Sodium::FFI::unpad($padded);
17+
```
18+
19+
# DESCRIPTION
20+
21+
[Sodium::FFI](https://metacpan.org/pod/Sodium%3A%3AFFI) is a set of Perl bindings for the [LibSodium](https://doc.libsodium.org/)
22+
C library. These bindings have been created using FFI via [FFI::Platypus](https://metacpan.org/pod/FFI%3A%3APlatypus) to make
23+
building and maintaining the bindings easier than was done via [Crypt::NaCl::Sodium](https://metacpan.org/pod/Crypt%3A%3ANaCl%3A%3ASodium).
24+
While we also intend to fixup [Crypt::NaCl::Sodium](https://metacpan.org/pod/Crypt%3A%3ANaCl%3A%3ASodium) so that it can use newer versions
25+
of LibSodium, the FFI method is faster to build and release.
26+
27+
# Utility/Helper Functions
28+
29+
LibSodium provides a few [Utility/Helper Functions](https://doc.libsodium.org/helpers)
30+
to assist you in getting your data ready for encryption, decryption, or hashing.
31+
32+
## sodium\_add
33+
34+
```perl
35+
use Sodium::FFI qw(sodium_add);
36+
my $left = "111";
37+
$left = sodium_add($left, 111);
38+
say $left; # bbb
39+
```
40+
41+
The [sodium\_add](https://doc.libsodium.org/helpers#adding-large-numbers)
42+
function adds 2 large numbers.
43+
44+
## sodium\_bin2hex
45+
46+
```perl
47+
use Sodium::FFI qw(sodium_bin2hex);
48+
my $binary = "ABC";
49+
my $hex = sodium_bin2hex($binary);
50+
say $hex; # 414243
51+
```
52+
53+
The [sodium\_bin2hex](https://doc.libsodium.org/helpers#hexadecimal-encoding-decoding)
54+
function takes a binary string and turns it into a hex string.
55+
56+
## sodium\_compare
57+
58+
```perl
59+
use Sodium::FFI qw(sodium_compare);
60+
say sodium_compare("\x01", "\x02"); # -1
61+
say sodium_compare("\x02", "\x01"); # 1
62+
say sodium_compare("\x01", "\x01"); # 0
63+
```
64+
65+
The [sodium\_compare](https://doc.libsodium.org/helpers#comparing-large-numbers)
66+
function compares two large numbers encoded in little endian format.
67+
Results in `-1` when `$left < $right`
68+
Results in `0` when `$left eq $right`
69+
Results in `1` when `$left > $right`
70+
71+
## sodium\_hex2bin
72+
73+
```perl
74+
use Sodium::FFI qw(sodium_hex2bin);
75+
my $hex = "414243";
76+
my $bin = sodium_hex2bin($hex);
77+
say $bin; # ABC
78+
```
79+
80+
The [sodium\_hex2bin](https://doc.libsodium.org/helpers#hexadecimal-encoding-decoding)
81+
function takes a hex string and turns it into a binary string.
82+
83+
## sodium\_increment
84+
85+
```perl
86+
use Sodium::FFI qw(sodium_increment);
87+
my $x = "\x01";
88+
$x = sodium_increment($x); # "\x02";
89+
```
90+
91+
The [sodium\_increment](https://doc.libsodium.org/helpers#incrementing-large-numbers)
92+
function takes an arbitrarily long unsigned number and increments it.
93+
94+
## sodium\_library\_minimal
95+
96+
```perl
97+
use Sodium::FFI qw(sodium_library_minimal);
98+
say sodium_library_minimal; # 0 or 1
99+
```
100+
101+
The `sodium_library_minimal` function lets you know if this is a minimal version.
102+
103+
## sodium\_library\_version\_major
104+
105+
```perl
106+
use Sodium::FFI qw(sodium_library_version_major);
107+
say sodium_library_version_major; # 10
108+
```
109+
110+
The `sodium_library_version_major` function returns the major version of the library.
111+
112+
## sodium\_library\_version\_minor
113+
114+
```perl
115+
use Sodium::FFI qw(sodium_library_version_minor);
116+
say sodium_library_version_minor; # 3
117+
```
118+
119+
The `sodium_library_version_minor` function returns the minor version of the library.
120+
121+
## sodium\_pad
122+
123+
```perl
124+
use Sodium::FFI qw(sodium_pad);
125+
say sodium_pad; # 3
9126
```
10127

128+
The [sodium\_pad](https://doc.libsodium.org/padding) function adds
129+
padding data to a buffer in order to extend its total length to a
130+
multiple of blocksize.
131+
132+
## sodium\_version\_string
133+
134+
```perl
135+
use Sodium::FFI qw(sodium_version_string);
136+
say sodium_version_string; # 1.0.18
137+
```
138+
139+
The `sodium_version_string` function returns the stringified version information
140+
for the version of LibSodium that you have installed.
141+
11142
# COPYRIGHT
12143

13144
```

lib/Sodium/FFI.pm

+117-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ our %function = (
3535
my ($xsub, $bin_string1, $bin_string2, $len) = @_;
3636
return unless $bin_string1 && $bin_string2;
3737
$len //= length($bin_string1);
38-
$xsub->($bin_string1, $bin_string2, $len);
38+
my $copy = substr($bin_string1, 0);
39+
$xsub->($copy, $bin_string2, $len);
40+
return $copy;
3941
}
4042
],
4143

@@ -98,7 +100,9 @@ our %function = (
98100
my ($xsub, $bin_string, $len) = @_;
99101
return unless $bin_string;
100102
$len //= length($bin_string);
101-
$xsub->($bin_string, $len);
103+
my $copy = substr($bin_string, 0);
104+
$xsub->($copy, $len);
105+
return $copy;
102106
}
103107
],
104108

@@ -261,6 +265,117 @@ Sodium::FFI - FFI implementation of libsodium
261265
=head1 SYNOPSIS
262266
263267
use strict;
268+
use warnings;
269+
use v5.34;
270+
271+
use Sodium::FFI ();
272+
273+
my $text = "1234";
274+
my $padded = Sodium::FFI::pad($text, 16);
275+
say Sodium::FFI::unpad($padded);
276+
277+
=head1 DESCRIPTION
278+
279+
L<Sodium::FFI> is a set of Perl bindings for the L<LibSodium|https://doc.libsodium.org/>
280+
C library. These bindings have been created using FFI via L<FFI::Platypus> to make
281+
building and maintaining the bindings easier than was done via L<Crypt::NaCl::Sodium>.
282+
While we also intend to fixup L<Crypt::NaCl::Sodium> so that it can use newer versions
283+
of LibSodium, the FFI method is faster to build and release.
284+
285+
=head1 Utility/Helper Functions
286+
287+
LibSodium provides a few L<Utility/Helper Functions|https://doc.libsodium.org/helpers>
288+
to assist you in getting your data ready for encryption, decryption, or hashing.
289+
290+
=head2 sodium_add
291+
292+
use Sodium::FFI qw(sodium_add);
293+
my $left = "111";
294+
$left = sodium_add($left, 111);
295+
say $left; # bbb
296+
297+
The L<sodium_add|https://doc.libsodium.org/helpers#adding-large-numbers>
298+
function adds 2 large numbers.
299+
300+
=head2 sodium_bin2hex
301+
302+
use Sodium::FFI qw(sodium_bin2hex);
303+
my $binary = "ABC";
304+
my $hex = sodium_bin2hex($binary);
305+
say $hex; # 414243
306+
307+
The L<sodium_bin2hex|https://doc.libsodium.org/helpers#hexadecimal-encoding-decoding>
308+
function takes a binary string and turns it into a hex string.
309+
310+
=head2 sodium_compare
311+
312+
use Sodium::FFI qw(sodium_compare);
313+
say sodium_compare("\x01", "\x02"); # -1
314+
say sodium_compare("\x02", "\x01"); # 1
315+
say sodium_compare("\x01", "\x01"); # 0
316+
317+
The L<sodium_compare|https://doc.libsodium.org/helpers#comparing-large-numbers>
318+
function compares two large numbers encoded in little endian format.
319+
Results in C<-1> when C<< $left < $right >>
320+
Results in C<0> when C<$left eq $right>
321+
Results in C<1> when C<< $left > $right >>
322+
323+
=head2 sodium_hex2bin
324+
325+
use Sodium::FFI qw(sodium_hex2bin);
326+
my $hex = "414243";
327+
my $bin = sodium_hex2bin($hex);
328+
say $bin; # ABC
329+
330+
The L<sodium_hex2bin|https://doc.libsodium.org/helpers#hexadecimal-encoding-decoding>
331+
function takes a hex string and turns it into a binary string.
332+
333+
=head2 sodium_increment
334+
335+
use Sodium::FFI qw(sodium_increment);
336+
my $x = "\x01";
337+
$x = sodium_increment($x); # "\x02";
338+
339+
The L<sodium_increment|https://doc.libsodium.org/helpers#incrementing-large-numbers>
340+
function takes an arbitrarily long unsigned number and increments it.
341+
342+
=head2 sodium_library_minimal
343+
344+
use Sodium::FFI qw(sodium_library_minimal);
345+
say sodium_library_minimal; # 0 or 1
346+
347+
The C<sodium_library_minimal> function lets you know if this is a minimal version.
348+
349+
=head2 sodium_library_version_major
350+
351+
use Sodium::FFI qw(sodium_library_version_major);
352+
say sodium_library_version_major; # 10
353+
354+
The C<sodium_library_version_major> function returns the major version of the library.
355+
356+
=head2 sodium_library_version_minor
357+
358+
use Sodium::FFI qw(sodium_library_version_minor);
359+
say sodium_library_version_minor; # 3
360+
361+
The C<sodium_library_version_minor> function returns the minor version of the library.
362+
363+
=head2 sodium_pad
364+
365+
use Sodium::FFI qw(sodium_pad);
366+
say sodium_pad; # 3
367+
368+
The L<sodium_pad|https://doc.libsodium.org/padding> function adds
369+
padding data to a buffer in order to extend its total length to a
370+
multiple of blocksize.
371+
372+
=head2 sodium_version_string
373+
374+
use Sodium::FFI qw(sodium_version_string);
375+
say sodium_version_string; # 1.0.18
376+
377+
The C<sodium_version_string> function returns the stringified version information
378+
for the version of LibSodium that you have installed.
264379
265380
=head1 COPYRIGHT
266381

t/00-report-prereqs.t

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use strict;
44
use warnings;
55

6-
# This test was generated by Dist::Zilla::Plugin::Test::ReportPrereqs 0.027
6+
# This test was generated by Dist::Zilla::Plugin::Test::ReportPrereqs 0.028
77

88
use Test::More tests => 1;
99

@@ -188,6 +188,6 @@ if ( @dep_errors ) {
188188
);
189189
}
190190

191-
pass;
191+
pass('Reported prereqs');
192192

193193
# vim: ts=4 sts=4 sw=4 et:

t/11-utils.t

+10-4
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,17 @@ is($readable, 'cafe6942', "hex2bin: maxlen 4, ignore ': ': readable; Cafe : 6942
3535
# sodium_add, sodium_increment
3636
{
3737
my $left = "\xFF\xFF\x80\x01\x02\x03\x04\x05\x06\x07\x08";
38-
sodium_increment($left);
38+
is(sodium_bin2hex($left), 'ffff800102030405060708', 'bin2hex: Got the right answer');
39+
$left = sodium_increment($left);
3940
is(sodium_bin2hex($left), '0000810102030405060708', 'increment, bin2hex: Got the right answer');
4041
my $right = "\x01\x02\x03\x04\x05\x06\x07\x08\xFA\xFB\xFC";
41-
sodium_add($left, $right);
42+
$left = sodium_add($left, $right);
4243
is(sodium_bin2hex($left), '0102840507090b0d000305', 'add, bin2hex: Got the right answer');
44+
my $foo = 111;
45+
is(sodium_add($foo, "111"), "bbb", 'add: non-lvalue test');
46+
is($foo, 111, 'left side was unaltered');
47+
$foo = sodium_add($foo, 111);
48+
is($foo, 'bbb', 'sodium_add: right value');
4349
}
4450

4551
# sodium_compare
@@ -48,9 +54,9 @@ SKIP: {
4854
my $v1 = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
4955
my $v2 = "\x02\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
5056
is(sodium_compare($v1, $v2), -1, 'sodium_compare: v1 < v2');
51-
sodium_increment($v1);
57+
$v1 = sodium_increment($v1);
5258
is(sodium_compare($v1, $v2), 0, 'sodium_compare: increment sets v1 == v2');
53-
sodium_increment($v1);
59+
$v1 = sodium_increment($v1);
5460
is(sodium_compare($v1, $v2), 1, 'sodium_compare: increment sets v1 > v2');
5561
};
5662

0 commit comments

Comments
 (0)