This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/op/crypt.t: Perform SHA-256 algorithm if default one is disabled
[perl5.git] / t / op / crypt.t
CommitLineData
69026470 1#!./perl -w
7b48dbf9
MS
2
3BEGIN {
4 chdir 't' if -d 't';
69026470 5 @INC = qw(. ../lib);
7b48dbf9
MS
6}
7
7b48dbf9 8BEGIN {
69026470
JH
9 use Config;
10
1ae3d757 11 require "./test.pl";
7b48dbf9
MS
12
13 if( !$Config{d_crypt} ) {
69026470 14 skip_all("crypt unimplemented");
7b48dbf9
MS
15 }
16 else {
f2791508 17 plan(tests => 4);
7b48dbf9
MS
18 }
19}
20
85c16d83
JH
21# Can't assume too much about the string returned by crypt(),
22# and about how many bytes of the encrypted (really, hashed)
23# string matter.
24#
25# HISTORICALLY the results started with the first two bytes of the salt,
26# followed by 11 bytes from the set [./0-9A-Za-z], and only the first
27# eight characters mattered, but those are probably no more safe
28# bets, given alternative encryption/hashing schemes like MD5,
29# C2 (or higher) security schemes, and non-UNIX platforms.
d3e5298a
P
30#
31# On platforms implementing FIPS mode, using a weak algorithm (including
32# the default triple-DES algorithm) causes crypt(3) to return a null
33# pointer, which Perl converts into undef. We assume for now that all
34# such platforms support glibc-style selection of a different hashing
35# algorithm.
36my $alg = ''; # Use default algorithm
37if ( !defined(crypt("ab", "cd")) ) {
38 $alg = '$5$'; # Use SHA-256
39}
85c16d83 40
dcd1d1bf 41SKIP: {
d3e5298a
P
42 skip ("VOS crypt ignores salt.", 1) if ($^O eq 'vos');
43 ok(substr(crypt("ab", $alg."cd"), 2) ne substr(crypt("ab", $alg."ce"), 2),
44 "salt makes a difference");
dcd1d1bf 45}
85c16d83 46
f2791508
JH
47$a = "a\xFF\x{100}";
48
d3e5298a 49eval {$b = crypt($a, $alg."cd")};
f2791508
JH
50like($@, qr/Wide character in crypt/, "wide characters ungood");
51
52chop $a; # throw away the wide character
53
d3e5298a 54eval {$b = crypt($a, $alg."cd")};
f2791508 55is($@, '', "downgrade to eight bit characters");
d3e5298a 56is($b, crypt("a\xFF", $alg."cd"), "downgrade results agree");
f2791508 57