File: URandom.pm

package info (click to toggle)
libuuid-urandom-perl 0.001-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 180 kB
  • sloc: perl: 180; makefile: 2
file content (199 lines) | stat: -rw-r--r-- 3,845 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use 5.008;
use strict;
use warnings;

package UUID::URandom;
# ABSTRACT: UUIDs based on /dev/urandom or the Windows Crypto API

our $VERSION = '0.001';

use Exporter 5.57 qw/import/;
use Crypt::URandom 0.36 ();

our @EXPORT_OK = qw(
  create_uuid
  create_uuid_hex
  create_uuid_string
);

#pod =func create_uuid
#pod
#pod     my $uuid = create_uuid();
#pod
#pod     # "\x95\x5a\xe4\x96\x8b\xb2\x45\x0b\x9c\x7e\x99\xf5\x01\xdf\x90\xfe"
#pod
#pod This returns a new UUID as a 16 byte 'binary' string.
#pod
#pod =cut

sub create_uuid {
    my $uuid = Crypt::URandom::urandom(16);
    vec( $uuid, 13, 4 ) = 0x4; # set UUID version
    vec( $uuid, 35, 2 ) = 0x2; # set UUID variant
    return $uuid;
}

#pod =func create_uuid_hex
#pod
#pod     my $uuid = create_uuid_hex();
#pod
#pod     # "955ae4968bb2450b9c7e99f501df90fe"
#pod
#pod This returns a new UUID as a 32-byte hexadecimal string.
#pod
#pod =cut

sub create_uuid_hex {
    return unpack( "H*", create_uuid() );
}

#pod =func create_uuid_string
#pod
#pod     my $uuid = create_uuid_string();
#pod
#pod     # "955ae496-8bb2-450b-9c7e-99f501df90fe"
#pod
#pod This returns a new UUID in the 36-byte RFC-4122 canonical string
#pod representation.  (N.B. The canonical representation is lower-case.)
#pod
#pod =cut

sub create_uuid_string {
    return join "-", unpack( "H8H4H4H4H12", create_uuid() );
}

1;


# vim: ts=4 sts=4 sw=4 et tw=75:

__END__

=pod

=encoding UTF-8

=head1 NAME

UUID::URandom - UUIDs based on /dev/urandom or the Windows Crypto API

=head1 VERSION

version 0.001

=head1 SYNOPSIS

    use UUID::URandom qw/create_uuid/;

    my $uuid = create_uuid();

=head1 DESCRIPTION

This module provides a portable, secure generator of
L<RFC-4122|https://tools.ietf.org/html/rfc4122> version 4
(random) UUIDs.  It is a thin wrapper around L<Crypt::URandom> to set
the UUID version and variant bits required by the RFC.

=head1 USAGE

No functions are exported by default.

=head1 FUNCTIONS

=head2 create_uuid

    my $uuid = create_uuid();

    # "\x95\x5a\xe4\x96\x8b\xb2\x45\x0b\x9c\x7e\x99\xf5\x01\xdf\x90\xfe"

This returns a new UUID as a 16 byte 'binary' string.

=head2 create_uuid_hex

    my $uuid = create_uuid_hex();

    # "955ae4968bb2450b9c7e99f501df90fe"

This returns a new UUID as a 32-byte hexadecimal string.

=head2 create_uuid_string

    my $uuid = create_uuid_string();

    # "955ae496-8bb2-450b-9c7e-99f501df90fe"

This returns a new UUID in the 36-byte RFC-4122 canonical string
representation.  (N.B. The canonical representation is lower-case.)

=begin Pod::Coverage




=end Pod::Coverage

=head1 FORK AND THREAD SAFETY

The underlying L<Crypt::URandom> is believed to be fork and thread safe.

=head1 SEE ALSO

There are a number of other modules that provide version 4 UUIDs.  Many
rely on insecure or non-crypto-strength random number generators.

=over 4

=item *

L<Data::GUID::Any>

=item *

L<Data::UUID::LibUUID>

=item *

L<UUID>

=item *

L<UUID::Tiny>

=item *

L<Data::UUID::MT>

=back

=for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan

=head1 SUPPORT

=head2 Bugs / Feature Requests

Please report any bugs or feature requests through the issue tracker
at L<https://github.com/dagolden/UUID-URandom/issues>.
You will be notified automatically of any progress on your issue.

=head2 Source Code

This is open source software.  The code repository is available for
public review and contribution under the terms of the license.

L<https://github.com/dagolden/UUID-URandom>

  git clone https://github.com/dagolden/UUID-URandom.git

=head1 AUTHOR

David Golden <dagolden@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2018 by David Golden.

This is free software, licensed under:

  The Apache License, Version 2.0, January 2004

=cut