xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/op/crypt.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl -w
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateBEGIN {
4*0Sstevel@tonic-gate    chdir 't' if -d 't';
5*0Sstevel@tonic-gate    @INC = qw(. ../lib);
6*0Sstevel@tonic-gate}
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gateBEGIN {
9*0Sstevel@tonic-gate    use Config;
10*0Sstevel@tonic-gate
11*0Sstevel@tonic-gate    require "test.pl";
12*0Sstevel@tonic-gate
13*0Sstevel@tonic-gate    if( !$Config{d_crypt} ) {
14*0Sstevel@tonic-gate        skip_all("crypt unimplemented");
15*0Sstevel@tonic-gate    }
16*0Sstevel@tonic-gate    else {
17*0Sstevel@tonic-gate        plan(tests => 4);
18*0Sstevel@tonic-gate    }
19*0Sstevel@tonic-gate}
20*0Sstevel@tonic-gate
21*0Sstevel@tonic-gate# Can't assume too much about the string returned by crypt(),
22*0Sstevel@tonic-gate# and about how many bytes of the encrypted (really, hashed)
23*0Sstevel@tonic-gate# string matter.
24*0Sstevel@tonic-gate#
25*0Sstevel@tonic-gate# HISTORICALLY the results started with the first two bytes of the salt,
26*0Sstevel@tonic-gate# followed by 11 bytes from the set [./0-9A-Za-z], and only the first
27*0Sstevel@tonic-gate# eight characters mattered, but those are probably no more safe
28*0Sstevel@tonic-gate# bets, given alternative encryption/hashing schemes like MD5,
29*0Sstevel@tonic-gate# C2 (or higher) security schemes, and non-UNIX platforms.
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gateSKIP: {
32*0Sstevel@tonic-gate	skip ("VOS crypt ignores salt.", 1) if ($^O eq 'vos');
33*0Sstevel@tonic-gate	ok(substr(crypt("ab", "cd"), 2) ne substr(crypt("ab", "ce"), 2), "salt makes a difference");
34*0Sstevel@tonic-gate}
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate$a = "a\xFF\x{100}";
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gateeval {$b = crypt($a, "cd")};
39*0Sstevel@tonic-gatelike($@, qr/Wide character in crypt/, "wide characters ungood");
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gatechop $a; # throw away the wide character
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gateeval {$b = crypt($a, "cd")};
44*0Sstevel@tonic-gateis($@, '',                   "downgrade to eight bit characters");
45*0Sstevel@tonic-gateis($b, crypt("a\xFF", "cd"), "downgrade results agree");
46*0Sstevel@tonic-gate
47