1 |
stefan |
1.1 |
package OpenSSL::Digest; |
2 |
|
|
|
3 |
|
|
=head1 NAME |
4 |
|
|
|
5 |
|
|
OpenSSL::Digest -- Access to OpenSSL digest functions |
6 |
|
|
|
7 |
|
|
=head1 SYNOPSIS |
8 |
|
|
|
9 |
|
|
use OpenSSL::Digest; |
10 |
|
|
|
11 |
|
|
=head1 DESCRIPTION |
12 |
|
|
|
13 |
|
|
Foo/bar. |
14 |
|
|
|
15 |
|
|
=over 4 |
16 |
|
|
|
17 |
|
|
=cut |
18 |
|
|
|
19 |
|
|
use OpenSSL; |
20 |
|
|
|
21 |
|
|
our $VERSION = '0.06'; |
22 |
|
|
use base Exporter; |
23 |
|
|
@EXPORT_OK = qw(digest digest_hex digest_base64 new_digest enum_digest |
24 |
|
|
md4 md5 sha sha1 dss dss1 mdc2 ripemd160 md2 |
25 |
|
|
md4_hex md5_hex sha_hex sha1_hex dss_hex dss1_hex mdc2_hex ripemd160_hex md2_hex |
26 |
|
|
md4_base64 md5_base64 sha_base64 sha1_base64 dss_base64 dss1_base64 mdc2_base64 ripemd160_base64 md2_base64 |
27 |
|
|
new_md4 new_md5 new_sha new_sha1 new_dss new_dss1 new_mdc2 new_ripemd160 new_md2 |
28 |
|
|
); |
29 |
|
|
|
30 |
|
|
our %dig_hash_hex; |
31 |
|
|
our %dig_hash_base64; |
32 |
|
|
our %dig_hash; |
33 |
|
|
our %dig_hash_new; |
34 |
|
|
our @dignames; |
35 |
|
|
|
36 |
|
|
for (qw(md4 md5 sha sha1 dss dss1 mdc2 ripemd160 md2)) { |
37 |
|
|
my $r = '\&OpenSSL::Digest::'; |
38 |
|
|
$dig_hash_hex{$_} = eval $r.$_."_hex"; |
39 |
|
|
$dig_hash_base64{$_} = eval $r.$_."_base64"; |
40 |
|
|
$dig_hash_new{$_} = eval $r.'new_'.$_; |
41 |
|
|
$dig_hash{$_} = eval $r.$_; |
42 |
|
|
push @dignames, $_; |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
=back |
46 |
|
|
|
47 |
|
|
=head1 FUNCTIONS |
48 |
|
|
|
49 |
|
|
=over 4 |
50 |
|
|
|
51 |
|
|
=item @digs = enum_digest(); |
52 |
|
|
|
53 |
|
|
Returns a list of supported digest. |
54 |
|
|
(sha1, md5, md2, md4, sha, mdc2, ripemd160, dss, dss1) |
55 |
|
|
|
56 |
|
|
=cut |
57 |
|
|
|
58 |
|
|
sub enum_digest() |
59 |
|
|
{ |
60 |
|
|
@dignames; |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
|
64 |
|
|
=item $hexdigest = digest_hex($digname, $string, ...) |
65 |
|
|
|
66 |
|
|
returns $digname digest over $string hex encoded |
67 |
|
|
|
68 |
|
|
|
69 |
|
|
=cut |
70 |
|
|
|
71 |
|
|
sub digest_hex($;@) { |
72 |
|
|
my $f = shift; |
73 |
|
|
$dig_hash_hex{$f}(join '', @_) |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
=item $bindigest = digest($digname, $string, ...) |
77 |
|
|
|
78 |
|
|
returns $digname digest over $string raw |
79 |
|
|
|
80 |
|
|
=cut |
81 |
|
|
|
82 |
|
|
sub digest($;@) { |
83 |
|
|
my $f = shift; |
84 |
|
|
$dig_hash{$f}(join '', @_) |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
=item $base64_digest = digest_base64($digname, $string, ...) |
88 |
|
|
|
89 |
|
|
returns $digname digest over $string base64 encoded |
90 |
|
|
|
91 |
|
|
=cut |
92 |
|
|
|
93 |
|
|
sub digest_base64($;@) { |
94 |
|
|
my $f = shift; |
95 |
|
|
$dig_hash_base64{$f}(join '', @_) |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
=item $hex_digest = {digestname}_hex($string) |
99 |
|
|
|
100 |
|
|
Direct access to low level function. |
101 |
|
|
|
102 |
|
|
=item $raw_digest = {digestname}($string) |
103 |
|
|
|
104 |
|
|
Direct access to low level function. |
105 |
|
|
|
106 |
|
|
=item $base64_digest = {digestname}_base64($string) |
107 |
|
|
|
108 |
|
|
Direct access to low level function. |
109 |
|
|
|
110 |
|
|
=item $ctx = {digestname}_new |
111 |
|
|
|
112 |
|
|
Access to classical stream based approach. |
113 |
|
|
|
114 |
|
|
=item $ctx = new_digest($digname) |
115 |
|
|
|
116 |
|
|
Access to classical stream based approach. |
117 |
|
|
|
118 |
|
|
=cut |
119 |
|
|
|
120 |
|
|
sub new_digest($) { |
121 |
|
|
$dig_hash_new{$_[0]}(); |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
=item $ctx->update($string) |
125 |
|
|
|
126 |
|
|
Adds $string to the digest |
127 |
|
|
|
128 |
|
|
=item $ctx->final |
129 |
|
|
|
130 |
|
|
Returns the digest raw. |
131 |
|
|
|
132 |
|
|
=item $ctx->final_hex |
133 |
|
|
|
134 |
|
|
Returns the digest hex encoded. |
135 |
|
|
|
136 |
|
|
=item $ctx->final_base64 |
137 |
|
|
|
138 |
|
|
Returns the digest base64 encoded. |
139 |
|
|
|
140 |
|
|
|
141 |
|
|
=back |
142 |
|
|
|
143 |
|
|
=head1 SEE ALSO |
144 |
|
|
|
145 |
|
|
L<OpenSSL>, L<OpenSSL::HMAC>. |
146 |
|
|
|
147 |
|
|
=head1 AUTHOR |
148 |
|
|
|
149 |
|
|
Stefan Traby <stefan@hello-penguin.com> |
150 |
|
|
http://mindterm.plan9.de/ |
151 |
|
|
|
152 |
|
|
=cut |
153 |
|
|
|
154 |
|
|
1; |