| 1 | #!/usr/bin/perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | |
| 5 | use Test::More tests => 9; |
| 6 | BEGIN { use_ok('Authen::Passphrase::Scrypt') }; |
| 7 | |
| 8 | # Vectors in the scrypt paper |
| 9 | my @vectors = ( |
| 10 | ['', '', 4, 1, 1, 64, '77d6576238657b203b19ca42c18a0497f16b4844e3074ae8dfdffa3fede21442fcd0069ded0948f8326a753a0fc81f17e8d3e0fb2e0d3628cf35e20c38d18906'], |
| 11 | ['password', 'NaCl', 10, 8, 16, 64, 'fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640'], |
| 12 | ['pleaseletmein', 'SodiumChloride', 14, 8, 1, 64, '7023bdcb3afd7348461c06cd81fd38ebfda8fbba904f8e3ea9b543f6545da1f2d5432955613f0fcf62d49705242a9af9e61e85dc0d651e40dfcf017b45575887'], |
| 13 | # Vector 4 omitted for performance reasons |
| 14 | ); |
| 15 | |
| 16 | for (1 .. @vectors) { |
| 17 | my ($pw, $salt, $logN, $r, $p, $len, $expected) = @{$vectors[$_ - 1]}; |
| 18 | my $result = crypto_scrypt $pw, $salt, (1 << $logN), $r, $p, $len; |
| 19 | $result = unpack 'H*', $result; |
| 20 | is $result, $expected, "Test vector $_" |
| 21 | } |
| 22 | |
| 23 | my $x = Authen::Passphrase::Scrypt->new({ |
| 24 | passphrase => 'password1' |
| 25 | }); |
| 26 | |
| 27 | ok $x->match('password1'), 'new + match'; |
| 28 | ok !$x->match('password2'), 'new + match'; |
| 29 | |
| 30 | my $test_rfc2307 = '{SCRYPT}c2NyeXB0AAwAAAAIAAAAAZ/+bp8gWcTZgEC7YQZeLLyxFeKRRdDkwbaGeFC0NkdUr/YFAWY/UwdOH4i/PxW48fXeXBDOTvGWtS3lLUgzNM0PlJbXhMOGd2bke0PvTSnW'; |
| 31 | |
| 32 | $x = Authen::Passphrase::Scrypt->from_rfc2307($test_rfc2307); |
| 33 | ok !$x->match('password1'), 'from_rfc2307 + match'; |
| 34 | ok $x->match('password2'), 'from_rfc2307 + match'; |
| 35 | |
| 36 | my $y = Authen::Passphrase::Scrypt->new({ |
| 37 | passphrase => 'password2', |
| 38 | salt => $x->salt, |
| 39 | logN => $x->logN, |
| 40 | r => 8, |
| 41 | }); |
| 42 | |
| 43 | is $y->as_rfc2307, $test_rfc2307, 'as_rfc2307'; |