| 1 | package UUID4::Tiny; | ||||||||||
| 2 | # ABSTRACT: Cryptographically secure v4 UUIDs for Linux x64 | ||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use 5.008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | our $VERSION = '0.002'; | |||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | use Carp qw/carp croak/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | our @ISA = qw/Exporter/; | |||||||||
| 13 | |||||||||||
| 14 | 1 | our @EXPORT_OK = qw/ | |||||||||
| 15 | create_uuid | ||||||||||
| 16 | create_uuid_string | ||||||||||
| 17 | is_uuid_string | ||||||||||
| 18 | is_uuid4_string | ||||||||||
| 19 | string_to_uuid | ||||||||||
| 20 | uuid_to_string | ||||||||||
| 21 | /; | ||||||||||
| 22 | |||||||||||
| 23 | 1 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||
| 24 | |||||||||||
| 25 | use constant { | ||||||||||
| 26 | 1 | GRND_NONBLOCK => 0x0001, | |||||||||
| 27 | RANDOM_BYTES => 16, | ||||||||||
| 28 | 1 | ▶ | }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | |||||||||||
| 30 | sub create_uuid { | ||||||||||
| 31 | 2 | ▶ | my $call = syscall( 318, my $uuid = "\0" x RANDOM_BYTES, | ||||||||
|
Subroutine
create_uuid: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 32 | RANDOM_BYTES, GRND_NONBLOCK ); | ||||||||||
| 33 | 2 | ▶ | croak "Syscall Error: $!" if $call == -1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 2 | ▶ | croak 'Insufficient Bytes Copied' if $call < RANDOM_BYTES; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | |||||||||||
| 36 | 2 | vec( $uuid, 13, 4 ) = 0x4; # version | |||||||||
| 37 | 2 | vec( $uuid, 35, 2 ) = 0x2; # variant | |||||||||
| 38 | |||||||||||
| 39 | 2 | return $uuid; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 1 | ▶ | sub create_uuid_string { uuid_to_string( create_uuid() ) } | ||||||||
|
Subroutine
create_uuid_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 43 | |||||||||||
| 44 | sub is_uuid_string { | ||||||||||
| 45 | 10 | ▶ | $_[0] =~ /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i; | ||||||||
|
Subroutine
is_uuid_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | sub is_uuid4_string { | ||||||||||
| 49 | 11 | ▶ | $_[0] =~ /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; | ||||||||
|
Subroutine
is_uuid4_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | sub string_to_uuid { | ||||||||||
| 53 | 4 | ▶ | my $string = shift; | ||||||||
|
Subroutine
string_to_uuid: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 54 | 4 | ▶ | if ( length $string == 16 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | # Emulates UUID::Tiny behavior to | ||||||||||
| 56 | # prevent accidental double conversion | ||||||||||
| 57 | 1 | carp 'Input not converted: assumed to be UUID bytes'; | |||||||||
| 58 | 1 | return $string; | |||||||||
| 59 | } | ||||||||||
| 60 | 3 | (my $hex = $string) =~ y/-//d; | |||||||||
| 61 | 3 | pack 'H*', $hex; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | sub uuid_to_string { | ||||||||||
| 65 | 6 | ▶ | my $uuid = shift; | ||||||||
|
Subroutine
uuid_to_string: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 66 | 6 | ▶ | if ( is_uuid_string $uuid ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | # Emulates UUID::Tiny behavior to | ||||||||||
| 68 | # prevent accidental double conversion | ||||||||||
| 69 | 1 | carp 'Input not converted: identified as UUID string'; | |||||||||
| 70 | 1 | return $uuid; | |||||||||
| 71 | } | ||||||||||
| 72 | 5 | join '-', unpack 'H8H4H4H4H12', $uuid; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 1 | 1; | |||||||||
| 76 | |||||||||||
| 77 | __END__ | ||||||||||