4 | 4 |
|
5 | 5 |
require Exporter; our @ISA = qw(Exporter); ### use Exporter 5.57 'import';
|
6 | 6 |
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)] );
|
8 | 8 |
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
|
9 | 9 |
our @EXPORT = qw();
|
10 | 10 |
|
|
121 | 121 |
return $diff == 0;
|
122 | 122 |
}
|
123 | 123 |
|
|
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 |
|
124 | 146 |
### private functions
|
125 | 147 |
|
126 | 148 |
sub _name2mode {
|
|
277 | 299 |
|
278 | 300 |
Convert DER to PEM representation. Supports also password protected PEM data.
|
279 | 301 |
|
|
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 |
|
280 | 322 |
=head1 SEE ALSO
|
281 | 323 |
|
282 | 324 |
=over
|