Codebase list libcryptx-perl / 3aac476
new: Crypt::Misc functions random_v4uuid + is_v4uuid Karel Miko 7 years ago
3 changed file(s) with 51 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
1616
1717 0.031 2016/05/XX
1818 - new: RSA+ECC method export_key_jwk_thumbprint()
19 - new: Crypt::Misc functions random_v4uuid + is_v4uuid
1920 - fix: RSA+ECC export_key_jwk produces canonical JSON
2021 - fix: RSA+DSA public key export now produces PEM/DER compatible with openssl
2122 public keys exported be previous version can still be imported
44
55 require Exporter; our @ISA = qw(Exporter); ### use Exporter 5.57 'import';
66 use Carp 'croak';
7 our %EXPORT_TAGS = ( all => [qw(encode_b64 decode_b64 encode_b64u decode_b64u pem_to_der der_to_pem read_rawfile write_rawfile slow_eq)] );
7 our %EXPORT_TAGS = ( all => [qw(encode_b64 decode_b64 encode_b64u decode_b64u pem_to_der der_to_pem read_rawfile write_rawfile slow_eq is_v4uuid random_v4uuid)] );
88 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
99 our @EXPORT = qw();
1010
121121 return $diff == 0;
122122 }
123123
124 sub random_v4uuid() {
125 # Version 4 - random - UUID: xxxxxxxx-xxxx-4xxx-Yxxx-xxxxxxxxxxxx
126 # where x is any hexadecimal digit and Y is one of 8, 9, A, B (1000, 1001, 1010, 1011)
127 # e.g. f47ac10b-58cc-4372-a567-0e02b2c3d479
128 my $raw = random_bytes(16);
129 # xxxxxxxxxxxx4xxxYxxxxxxxxxxxxxxx
130 $raw &= pack("H*", "FFFFFFFFFFFF0FFFFFFFFFFFFFFFFFFF");
131 $raw |= pack("H*", "00000000000040000000000000000000");
132 $raw &= pack("H*", "FFFFFFFFFFFFFFFF3FFFFFFFFFFFFFFF"); # 0x3 == 0011b
133 $raw |= pack("H*", "00000000000000008000000000000000"); # 0x8 == 1000b
134 my $hex = unpack("H*", $raw);
135 $hex =~ s/^(.{8})(.{4})(.{4})(.{4})(.{12}).*$/$1-$2-$3-$4-$5/;
136 return $hex;
137 }
138
139 sub is_v4uuid($) {
140 my $uuid = shift;
141 return 0 if !$uuid;
142 return 1 if $uuid =~ /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
143 return 0;
144 }
145
124146 ### private functions
125147
126148 sub _name2mode {
277299
278300 Convert DER to PEM representation. Supports also password protected PEM data.
279301
302 =head2 random_v4uuid
303
304 I<Since: CryptX-0.031>
305
306 my $uuid = random_v4uuid();
307
308 Returns cryptographically strong Version 4 random UUID: C<xxxxxxxx-xxxx-4xxx-Yxxx-xxxxxxxxxxxx>
309 where C<x> is any hexadecimal digit and C<Y> is one of 8, 9, A, B (1000, 1001, 1010, 1011)
310 e.g. C<f47ac10b-58cc-4372-a567-0e02b2c3d479>.
311
312 =head2 is_v4uuid
313
314 I<Since: CryptX-0.031>
315
316 if (is_v4uuid($uuid)) {
317 ...
318 }
319
320 Checks the given C<$uuid> string whether it matches V4 UUID format and returns C<0> (mismatch) or C<1> (match).
321
280322 =head1 SEE ALSO
281323
282324 =over
00 use strict;
11 use warnings;
2 use Test::More tests => 15;
2 use Test::More tests => 17;
33
4 use Crypt::Misc qw(encode_b64 decode_b64 encode_b64u decode_b64u pem_to_der der_to_pem read_rawfile write_rawfile slow_eq);
4 use Crypt::Misc qw(encode_b64 decode_b64 encode_b64u decode_b64u pem_to_der der_to_pem read_rawfile write_rawfile slow_eq is_v4uuid random_v4uuid);
55
66 is(encode_b64(pack("H*","702fad4215a04a657f011d3ea5711879c696788c91d2")), "cC+tQhWgSmV/AR0+pXEYecaWeIyR0g==", "encode_b64");
77 is(unpack("H*", decode_b64("cC+tQhWgSmV/AR0+pXEYecaWeIyR0g==")), "702fad4215a04a657f011d3ea5711879c696788c91d2", "decode_b64");
2222 write_rawfile("tmp.$$.file", "a\nb\r\nc\rd\te");
2323 ok(slow_eq(read_rawfile("tmp.$$.file"), "a\nb\r\nc\rd\te"), "slow_eq + read_rawfile + write_rawfile");
2424 unlink "tmp.$$.file";
25
26 my $uuid = random_v4uuid;
27 ok($uuid, 'random_v4uuid');
28 ok(is_v4uuid($uuid), 'is_v4uuid');
29