Created
January 28, 2012 19:03
-
-
Save viliampucik/1695439 to your computer and use it in GitHub Desktop.
Perl Name-based UUID Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Digest::MD5 qw( md5 ); | |
# based on UUID::Tiny | |
sub uuid_v3 { | |
my ( $namespace, $name ) = @_; | |
$namespace =~ tr/\-//d; | |
$namespace = pack 'H*', $namespace; | |
my $uuid = md5( $namespace . $name ); | |
# set version 3 (MD5 name-based) UUID | |
substr $uuid, 6, 1, chr( ord( substr $uuid, 6, 1 ) & 0x0f | 0x30 ); | |
# set RFC 4122 variant | |
substr $uuid, 8, 1, chr( ord( substr $uuid, 8, 1 ) & 0x3f | 0x80 ); | |
return join '-', | |
map { unpack 'H*', $_ } | |
map { substr $uuid, 0, $_, '' } | |
( 4, 2, 2, 2, 6 ); | |
} | |
print uuid_v3( $ARGV[0], $ARGV[1] ), "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment