File: 003_create_digests.t

package info (click to toggle)
libdigest-bcrypt-perl 1.212-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 204 kB
  • sloc: perl: 305; makefile: 2
file content (184 lines) | stat: -rwxr-xr-x 6,674 bytes parent folder | download
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
use strict;
use warnings;

use Digest;
use Digest::Bcrypt;
use Test::More;
use Try::Tiny qw( try catch );

my $secret = "Super Secret Squirrel";
my $salt   = "   known salt   ";
my $cost   = 5;

# Object is reset after each hash is generated
my $direct = Digest::Bcrypt->new;
isa_ok($direct, 'Digest::Bcrypt', 'new: direct dist use syntax');
my $indirect = Digest->new('Bcrypt');
isa_ok($direct, 'Digest::Bcrypt', 'new: indirect dist use via Digest');

# direct binary
{
    my $res = try {
        $direct->add($secret);
        is($direct->{_buffer}, $secret, 'direct binary: buffer set correctly');
        $direct->salt($salt);
        is($direct->salt(), $salt, 'direct binary: salt set correctly');
        $direct->cost($cost);
        is($direct->cost(), '05', 'direct binary: cost set correctly');
        ok($direct->digest, "Creates Binary Digest");
        return '';
    }
    catch { return "Error: $_" };
    is($res,               '',    'direct binary: no errors trapped');
    is($direct->{_buffer}, '',    'direct binary: buffer emptied after');
    is($direct->salt(),    undef, 'direct binary: salt emptied after');
    is($direct->cost(),    undef, 'direct binary: cost empted after');
}

# direct hex
{
    my $res = try {
        $direct->add($secret);
        is($direct->{_buffer}, $secret, 'direct hex: buffer set correctly');
        $direct->salt($salt);
        is($direct->salt(), $salt, 'direct hex: salt set correctly');
        $direct->cost($cost);
        is($direct->cost(), '05', 'direct hex: cost set correctly');
        return $direct->hexdigest;
    }
    catch { return "Error: $_" };
    is(
        $res,
        '611d856adaf4357f2891de1e1024d9fa0e49b67ffc724d',
        "direct hex: proper value"
    );
    is($direct->{_buffer}, '',    'direct hex: buffer emptied after');
    is($direct->salt(),    undef, 'direct hex: salt emptied after');
    is($direct->cost(),    undef, 'direct hex: cost empted after');
}

# direct b64
{
    my $res = try {
        $direct->add($secret);
        is($direct->{_buffer}, $secret, 'direct b64: buffer set correctly');
        $direct->salt($salt);
        is($direct->salt(), $salt, 'direct b64: salt set correctly');
        $direct->cost($cost);
        is($direct->cost(), '05', 'direct b64: cost set correctly');
        return $direct->b64digest;
    }
    catch { return "Error: $_" };
    is($res, 'YR2Fatr0NX8okd4eECTZ+g5Jtn/8ck0', "direct b64: proper value");
    is($direct->{_buffer}, '',    'direct b64: buffer emptied after');
    is($direct->salt(),    undef, 'direct b64: salt emptied after');
    is($direct->cost(),    undef, 'direct b64: cost empted after');
}

# direct bcrypt_b64
{
    my $res = try {
        $direct->add($secret);
        is($direct->{_buffer}, $secret,
            'direct bcrypt_b64: buffer set correctly');
        $direct->salt($salt);
        is($direct->salt(), $salt, 'direct bcrypt_b64: salt set correctly');
        $direct->cost($cost);
        is($direct->cost(), '05', 'direct bcrypt_b64: cost set correctly');
        return $direct->bcrypt_b64digest;
    }
    catch { return "Error: $_" };
    is(
        $res,
        'WP0DYrpyLV6mib2cCARX8e3Hrl96aiy',
        "direct bcrypt_b64: proper value"
    );
    is($direct->{_buffer}, '',    'direct bcrypt_b64: buffer emptied after');
    is($direct->salt(),    undef, 'direct bcrypt_b64: salt emptied after');
    is($direct->cost(),    undef, 'direct bcrypt_b64: cost empted after');
}

# indirect binary
{
    my $res = try {
        $indirect->add($secret);
        is($indirect->{_buffer}, $secret,
            'indirect binary: buffer set correctly');
        $indirect->salt($salt);
        is($indirect->salt(), $salt, 'indirect binary: salt set correctly');
        $indirect->cost($cost);
        is($indirect->cost(), '05', 'indirect binary: cost set correctly');
        ok($indirect->digest, "indirect binary: digest created");
        return '';
    }
    catch { return "Error: $_" };
    is($res,                 '',    'indirect binary: no errors trapped');
    is($indirect->{_buffer}, '',    'indirect binary: buffer emptied after');
    is($indirect->salt(),    undef, 'indirect binary: salt emptied after');
    is($indirect->cost(),    undef, 'indirect binary: cost empted after');
}

# indirect hex
{
    my $res = try {
        $indirect->add($secret);
        is($indirect->{_buffer}, $secret, 'indirect hex: buffer set correctly');
        $indirect->salt($salt);
        is($indirect->salt(), $salt, 'indirect hex: salt set correctly');
        $indirect->cost($cost);
        is($indirect->cost(), '05', 'indirect hex: cost set correctly');
        return $indirect->hexdigest;
    }
    catch { return "Error: $_" };
    is(
        $res,
        '611d856adaf4357f2891de1e1024d9fa0e49b67ffc724d',
        "indirect hex: proper value"
    );
    is($indirect->{_buffer}, '',    'indirect hex: buffer emptied after');
    is($indirect->salt(),    undef, 'indirect hex: salt emptied after');
    is($indirect->cost(),    undef, 'indirect hex: cost empted after');
}

# indirect b64
{
    my $res = try {
        $indirect->add($secret);
        is($indirect->{_buffer}, $secret, 'indirect b64: buffer set correctly');
        $indirect->salt($salt);
        is($indirect->salt(), $salt, 'indirect b64: salt set correctly');
        $indirect->cost($cost);
        is($indirect->cost(), '05', 'indirect b64: cost set correctly');
        return $indirect->b64digest;
    }
    catch { return "Error: $_" };
    is($res, 'YR2Fatr0NX8okd4eECTZ+g5Jtn/8ck0', "indirect b64: proper value");
    is($indirect->{_buffer}, '',    'indirect b64: buffer emptied after');
    is($indirect->salt(),    undef, 'indirect b64: salt emptied after');
    is($indirect->cost(),    undef, 'indirect b64: cost empted after');
}

# indirect bcrypt_b64
{
    my $res = try {
        $indirect->add($secret);
        is($indirect->{_buffer}, $secret,
            'indirect bcrypt_b64: buffer set correctly');
        $indirect->salt($salt);
        is($indirect->salt(), $salt, 'indirect bcrypt_b64: salt set correctly');
        $indirect->cost($cost);
        is($indirect->cost(), '05', 'indirect bcrypt_b64: cost set correctly');
        return $indirect->bcrypt_b64digest;
    }
    catch { return "Error: $_" };
    is(
        $res,
        'WP0DYrpyLV6mib2cCARX8e3Hrl96aiy',
        "indirect bcrypt_b64: proper value"
    );
    is($indirect->{_buffer}, '', 'indirect bcrypt_b64: buffer emptied after');
    is($indirect->salt(), undef, 'indirect bcrypt_b64: salt emptied after');
    is($indirect->cost(), undef, 'indirect bcrypt_b64: cost empted after');
}

done_testing();