tuning XS
Karel Miko
6 years ago
52 | 52 | |
53 | 53 | SV * |
54 | 54 | digest(Crypt::Checksum::Adler32 self) |
55 | ALIAS: | |
56 | hexdigest = 1 | |
57 | intdigest = 2 | |
55 | 58 | CODE: |
56 | 59 | { |
57 | unsigned char hash[4]; | |
60 | int rv; | |
61 | unsigned char hash[4], out[8]; | |
62 | unsigned long outlen = 8; | |
63 | unsigned int ui32; | |
64 | ||
58 | 65 | adler32_finish(self, hash, 4); /* returns void */ |
59 | RETVAL = newSVpvn((char *) hash, 4); | |
66 | if (ix == 1) { | |
67 | rv = _base16_encode(hash, 4, out, &outlen); | |
68 | if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv)); | |
69 | RETVAL = newSVpvn(out, outlen); | |
70 | } | |
71 | else if (ix == 2) { | |
72 | LOAD32H(ui32, hash); | |
73 | RETVAL = newSVuv(ui32); | |
74 | } | |
75 | else { | |
76 | RETVAL = newSVpvn((char *) hash, 4); | |
77 | } | |
60 | 78 | } |
61 | 79 | OUTPUT: |
62 | 80 | RETVAL |
63 | 81 | |
64 | 82 | SV * |
65 | hexdigest(Crypt::Checksum::Adler32 self) | |
83 | adler32_data(...) | |
84 | ALIAS: | |
85 | adler32_data_hex = 1 | |
86 | adler32_data_int = 2 | |
66 | 87 | CODE: |
67 | 88 | { |
68 | unsigned long i; | |
69 | unsigned char hash[4]; | |
70 | char hash_hex[4*2 + 1]; | |
71 | adler32_finish(self, hash, 4); /* returns void */ | |
72 | hash_hex[0] = '\0'; | |
73 | for(i=0; i<4; i++) sprintf(&hash_hex[2*i], "%02x", hash[i]); | |
74 | RETVAL = newSVpvn(hash_hex, strlen(hash_hex)); | |
89 | adler32_state st; | |
90 | int rv, j; | |
91 | unsigned char hash[4], out[8], *in; | |
92 | unsigned long outlen = 8; | |
93 | unsigned int ui32; | |
94 | STRLEN inlen; | |
95 | ||
96 | adler32_init(&st); | |
97 | for(j = 0; j < items; j++) { | |
98 | in = (unsigned char *)SvPVbyte(ST(j), inlen); | |
99 | if (inlen > 0) { | |
100 | adler32_update(&st, in, (unsigned long)inlen); /* returns void */ | |
101 | } | |
102 | } | |
103 | adler32_finish(&st, hash, 4); /* returns void */ | |
104 | if (ix == 1) { | |
105 | rv = _base16_encode(hash, 4, out, &outlen); | |
106 | if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv)); | |
107 | RETVAL = newSVpvn(out, outlen); | |
108 | } | |
109 | else if (ix == 2) { | |
110 | LOAD32H(ui32, hash); | |
111 | RETVAL = newSVuv(ui32); | |
112 | } | |
113 | else { | |
114 | RETVAL = newSVpvn((char *) hash, 4); | |
115 | } | |
75 | 116 | } |
76 | 117 | OUTPUT: |
77 | 118 | RETVAL |
78 |
52 | 52 | |
53 | 53 | SV * |
54 | 54 | digest(Crypt::Checksum::CRC32 self) |
55 | ALIAS: | |
56 | hexdigest = 1 | |
57 | intdigest = 2 | |
55 | 58 | CODE: |
56 | 59 | { |
57 | unsigned char hash[4]; | |
60 | int rv; | |
61 | unsigned char hash[4], out[8]; | |
62 | unsigned long outlen = 8; | |
63 | unsigned int ui32; | |
64 | ||
58 | 65 | crc32_finish(self, hash, 4); /* returns void */ |
59 | RETVAL = newSVpvn((char *) hash, 4); | |
66 | if (ix == 1) { | |
67 | rv = _base16_encode(hash, 4, out, &outlen); | |
68 | if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv)); | |
69 | RETVAL = newSVpvn(out, outlen); | |
70 | } | |
71 | else if (ix == 2) { | |
72 | LOAD32H(ui32, hash); | |
73 | RETVAL = newSVuv(ui32); | |
74 | } | |
75 | else { | |
76 | RETVAL = newSVpvn((char *) hash, 4); | |
77 | } | |
60 | 78 | } |
61 | 79 | OUTPUT: |
62 | 80 | RETVAL |
63 | 81 | |
64 | 82 | SV * |
65 | hexdigest(Crypt::Checksum::CRC32 self) | |
83 | crc32_data(...) | |
84 | ALIAS: | |
85 | crc32_data_hex = 1 | |
86 | crc32_data_int = 2 | |
66 | 87 | CODE: |
67 | 88 | { |
68 | unsigned long i; | |
69 | unsigned char hash[4]; | |
70 | char hash_hex[4*2 + 1]; | |
71 | crc32_finish(self, hash, 4); /* returns void */ | |
72 | hash_hex[0] = '\0'; | |
73 | for(i=0; i<4; i++) sprintf(&hash_hex[2*i], "%02x", hash[i]); | |
74 | RETVAL = newSVpvn(hash_hex, strlen(hash_hex)); | |
89 | crc32_state st; | |
90 | int rv, j; | |
91 | unsigned char hash[4], out[8], *in; | |
92 | unsigned long outlen = 8; | |
93 | unsigned int ui32; | |
94 | STRLEN inlen; | |
95 | ||
96 | crc32_init(&st); | |
97 | for(j = 0; j < items; j++) { | |
98 | in = (unsigned char *)SvPVbyte(ST(j), inlen); | |
99 | if (inlen > 0) { | |
100 | crc32_update(&st, in, (unsigned long)inlen); /* returns void */ | |
101 | } | |
102 | } | |
103 | crc32_finish(&st, hash, 4); /* returns void */ | |
104 | if (ix == 1) { | |
105 | rv = _base16_encode(hash, 4, out, &outlen); | |
106 | if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv)); | |
107 | RETVAL = newSVpvn(out, outlen); | |
108 | } | |
109 | else if (ix == 2) { | |
110 | LOAD32H(ui32, hash); | |
111 | RETVAL = newSVuv(ui32); | |
112 | } | |
113 | else { | |
114 | RETVAL = newSVpvn((char *) hash, 4); | |
115 | } | |
75 | 116 | } |
76 | 117 | OUTPUT: |
77 | 118 | RETVAL |
78 |
3 | 3 | use warnings; |
4 | 4 | our $VERSION = '0.056_006'; |
5 | 5 | |
6 | use base qw(Crypt::AuthEnc Exporter); | |
6 | require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import'; | |
7 | 7 | our %EXPORT_TAGS = ( all => [qw( ccm_encrypt_authenticate ccm_decrypt_verify )] ); |
8 | 8 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
9 | 9 | our @EXPORT = qw(); |
11 | 11 | use Carp; |
12 | 12 | $Carp::Internal{(__PACKAGE__)}++; |
13 | 13 | use CryptX; |
14 | ||
15 | sub CLONE_SKIP { 1 } # prevent cloning | |
14 | 16 | |
15 | 17 | 1; |
16 | 18 |
3 | 3 | use warnings; |
4 | 4 | our $VERSION = '0.056_006'; |
5 | 5 | |
6 | use base qw(Crypt::AuthEnc Exporter); | |
6 | require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import'; | |
7 | 7 | our %EXPORT_TAGS = ( all => [qw( chacha20poly1305_encrypt_authenticate chacha20poly1305_decrypt_verify )] ); |
8 | 8 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
9 | 9 | our @EXPORT = qw(); |
11 | 11 | use Carp; |
12 | 12 | $Carp::Internal{(__PACKAGE__)}++; |
13 | 13 | use CryptX; |
14 | ||
15 | sub CLONE_SKIP { 1 } # prevent cloning | |
14 | 16 | |
15 | 17 | 1; |
16 | 18 |
3 | 3 | use warnings; |
4 | 4 | our $VERSION = '0.056_006'; |
5 | 5 | |
6 | use base qw(Crypt::AuthEnc Exporter); | |
6 | require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import'; | |
7 | 7 | our %EXPORT_TAGS = ( all => [qw( eax_encrypt_authenticate eax_decrypt_verify )] ); |
8 | 8 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
9 | 9 | our @EXPORT = qw(); |
15 | 15 | # obsolete, only for backwards compatibility |
16 | 16 | sub header_add { goto &adata_add } |
17 | 17 | sub aad_add { goto &adata_add } |
18 | ||
19 | sub CLONE_SKIP { 1 } # prevent cloning | |
18 | 20 | |
19 | 21 | 1; |
20 | 22 |
3 | 3 | use warnings; |
4 | 4 | our $VERSION = '0.056_006'; |
5 | 5 | |
6 | use base qw(Crypt::AuthEnc Exporter); | |
6 | require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import'; | |
7 | 7 | our %EXPORT_TAGS = ( all => [qw( gcm_encrypt_authenticate gcm_decrypt_verify )] ); |
8 | 8 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
9 | 9 | our @EXPORT = qw(); |
11 | 11 | use Carp; |
12 | 12 | $Carp::Internal{(__PACKAGE__)}++; |
13 | 13 | use CryptX; |
14 | ||
15 | sub CLONE_SKIP { 1 } # prevent cloning | |
14 | 16 | |
15 | 17 | 1; |
16 | 18 |
3 | 3 | use warnings; |
4 | 4 | our $VERSION = '0.056_006'; |
5 | 5 | |
6 | use base qw(Crypt::AuthEnc Exporter); | |
6 | require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import'; | |
7 | 7 | our %EXPORT_TAGS = ( all => [qw( ocb_encrypt_authenticate ocb_decrypt_verify )] ); |
8 | 8 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
9 | 9 | our @EXPORT = qw(); |
15 | 15 | # obsolete, only for backwards compatibility |
16 | 16 | sub aad_add { goto &adata_add } |
17 | 17 | sub blocksize { return 16 } |
18 | ||
19 | sub CLONE_SKIP { 1 } # prevent cloning | |
18 | 20 | |
19 | 21 | 1; |
20 | 22 |
3 | 3 | use warnings; |
4 | 4 | our $VERSION = '0.056_006'; |
5 | 5 | |
6 | sub CLONE_SKIP { 1 } # prevent cloning | |
6 | ### not used | |
7 | 7 | |
8 | 8 | 1; |
9 | 9 |
3 | 3 | use warnings; |
4 | 4 | our $VERSION = '0.056_006'; |
5 | 5 | |
6 | use base qw(Crypt::Checksum Exporter); | |
7 | our %EXPORT_TAGS = ( all => [qw( adler32_data adler32_data_hex adler32_data_int adler32_file adler32_file_hex adler32_file_int )] ); | |
8 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |
9 | our @EXPORT = qw(); | |
10 | ||
6 | 11 | use Carp; |
7 | 12 | $Carp::Internal{(__PACKAGE__)}++; |
8 | 13 | use CryptX; |
9 | 14 | |
10 | sub addfile { | |
11 | my ($self, $file) = @_; | |
12 | ||
13 | my $handle; | |
14 | if (ref(\$file) eq 'SCALAR') { #filename | |
15 | open($handle, "<", $file) || croak "FATAL: cannot open '$file': $!"; | |
16 | binmode($handle); | |
17 | } | |
18 | else { #handle | |
19 | $handle = $file | |
20 | } | |
21 | croak "FATAL: invalid handle" unless defined $handle; | |
22 | ||
23 | my $n; | |
24 | my $buf = ""; | |
25 | while (($n = read($handle, $buf, 32*1024))) { | |
26 | $self->add($buf) | |
27 | } | |
28 | croak "FATAL: read failed: $!" unless defined $n; | |
29 | ||
30 | return $self; | |
31 | } | |
32 | ||
33 | sub CLONE_SKIP { 1 } # prevent cloning | |
15 | sub adler32_file { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::Adler32->new->addfile(@_)->digest } | |
16 | sub adler32_file_hex { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::Adler32->new->addfile(@_)->hexdigest } | |
17 | sub adler32_file_int { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::Adler32->new->addfile(@_)->intdigest } | |
34 | 18 | |
35 | 19 | 1; |
36 | 20 | |
42 | 26 | |
43 | 27 | =head1 SYNOPSIS |
44 | 28 | |
29 | ### Functional interface: | |
30 | use Crypt::Checksum::Adler32 ':all'; | |
31 | ||
32 | # calculate Adler32 checksum from string/buffer | |
33 | $checksum_raw = adler32_data($data); | |
34 | $checksum_hex = adler32_data_hex($data); | |
35 | $checksum_int = adler32_data_int($data); | |
36 | # calculate Adler32 checksum from file | |
37 | $checksum_raw = adler32_file('filename.dat'); | |
38 | $checksum_hex = adler32_file_hex('filename.dat'); | |
39 | $checksum_int = adler32_file_int('filename.dat'); | |
40 | # calculate Adler32 checksum from filehandle | |
41 | $checksum_raw = adler32_file(*FILEHANDLE); | |
42 | $checksum_hex = adler32_file_hex(*FILEHANDLE); | |
43 | $checksum_int = adler32_file_int(*FILEHANDLE); | |
44 | ||
45 | ### OO interface: | |
45 | 46 | use Crypt::Checksum::Adler32; |
46 | 47 | |
47 | 48 | $d = Crypt::Checksum::Adler32->new; |
48 | 49 | $d->add('any data'); |
50 | $d->add('another data'); | |
49 | 51 | $d->addfile('filename.dat'); |
50 | 52 | $d->addfile(*FILEHANDLE); |
51 | $checksum_raw = $d->digest; # raw bytes | |
53 | $checksum_raw = $d->digest; # raw 4 bytes | |
52 | 54 | $checksum_hex = $d->hexdigest; # hexadecimal form |
55 | $checksum_int = $d->intdigest; # 32bit unsigned integer | |
53 | 56 | |
54 | 57 | =head1 DESCRIPTION |
55 | 58 | |
56 | Calculating Adler32 checksums (OO interface); | |
59 | Calculating Adler32 checksums. | |
57 | 60 | |
58 | I<Since: CryptX-0.032> | |
61 | I<Updated: v0.057> | |
62 | ||
63 | =head1 EXPORT | |
64 | ||
65 | Nothing is exported by default. | |
66 | ||
67 | You can export selected functions: | |
68 | ||
69 | use Crypt::Checksum::Adler32 qw(adler32_data adler32_data_hex adler32_data_int adler32_file adler32_file_hex adler32_file_int); | |
70 | ||
71 | Or all of them at once: | |
72 | ||
73 | use Crypt::Checksum::Adler32 ':all'; | |
74 | ||
75 | =head1 FUNCTIONS | |
76 | ||
77 | =head2 adler32_data | |
78 | ||
79 | Returns checksum as raw octects. | |
80 | ||
81 | $checksum_raw = adler32_data('data string'); | |
82 | #or | |
83 | $checksum_raw = adler32_data('any data', 'more data', 'even more data'); | |
84 | ||
85 | =head2 adler32_data_hex | |
86 | ||
87 | Returns checksum as a hexadecimal string. | |
88 | ||
89 | $checksum_hex = adler32_data_hex('data string'); | |
90 | #or | |
91 | $checksum_hex = adler32_data_hex('any data', 'more data', 'even more data'); | |
92 | ||
93 | =head2 adler32_data_int | |
94 | ||
95 | Returns checksum as unsigned 32bit integer. | |
96 | ||
97 | $checksum_hex = adler32_data_int('data string'); | |
98 | #or | |
99 | $checksum_hex = adler32_data_int('any data', 'more data', 'even more data'); | |
100 | ||
101 | =head2 adler32_file | |
102 | ||
103 | Returns checksum as raw octects. | |
104 | ||
105 | $checksum_raw = adler32_file('filename.dat'); | |
106 | #or | |
107 | $checksum_raw = adler32_file(*FILEHANDLE); | |
108 | ||
109 | =head2 adler32_file_hex | |
110 | ||
111 | Returns checksum as a hexadecimal string. | |
112 | ||
113 | $checksum_hex = adler32_file_hex('filename.dat'); | |
114 | #or | |
115 | $checksum_hex = adler32_file_hex(*FILEHANDLE); | |
116 | ||
117 | =head2 adler32_file_int | |
118 | ||
119 | Returns checksum as unsigned 32bit integer. | |
120 | ||
121 | $checksum_hex = adler32_file_int('filename.dat'); | |
122 | #or | |
123 | $checksum_hex = adler32_file_int(*FILEHANDLE); | |
59 | 124 | |
60 | 125 | =head1 METHODS |
61 | 126 | |
109 | 174 | |
110 | 175 | $result_hex = $d->hexdigest(); |
111 | 176 | |
177 | =head2 intdigest | |
178 | ||
179 | Returns the checksum encoded as unsigned 32bit integer. | |
180 | ||
181 | $result_int = $d->intdigest(); | |
182 | ||
112 | 183 | =head1 SEE ALSO |
113 | 184 | |
114 | 185 | =over |
115 | 186 | |
116 | =item * L<CryptX|CryptX>, L<Crypt::Checksum> | |
187 | =item * L<CryptX|CryptX> | |
117 | 188 | |
118 | 189 | =item * L<https://en.wikipedia.org/wiki/Adler-32> |
119 | 190 |
3 | 3 | use warnings; |
4 | 4 | our $VERSION = '0.056_006'; |
5 | 5 | |
6 | use base qw(Crypt::Checksum Exporter); | |
7 | our %EXPORT_TAGS = ( all => [qw( crc32_data crc32_data_hex crc32_data_int crc32_file crc32_file_hex crc32_file_int )] ); | |
8 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |
9 | our @EXPORT = qw(); | |
10 | ||
6 | 11 | use Carp; |
7 | 12 | $Carp::Internal{(__PACKAGE__)}++; |
8 | 13 | use CryptX; |
9 | 14 | |
10 | sub addfile { | |
11 | my ($self, $file) = @_; | |
12 | ||
13 | my $handle; | |
14 | if (ref(\$file) eq 'SCALAR') { #filename | |
15 | open($handle, "<", $file) || croak "FATAL: cannot open '$file': $!"; | |
16 | binmode($handle); | |
17 | } | |
18 | else { #handle | |
19 | $handle = $file | |
20 | } | |
21 | croak "FATAL: invalid handle" unless defined $handle; | |
22 | ||
23 | my $n; | |
24 | my $buf = ""; | |
25 | while (($n = read($handle, $buf, 32*1024))) { | |
26 | $self->add($buf) | |
27 | } | |
28 | croak "FATAL: read failed: $!" unless defined $n; | |
29 | ||
30 | return $self; | |
31 | } | |
32 | ||
33 | sub CLONE_SKIP { 1 } # prevent cloning | |
15 | sub crc32_file { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::CRC32->new->addfile(@_)->digest } | |
16 | sub crc32_file_hex { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::CRC32->new->addfile(@_)->hexdigest } | |
17 | sub crc32_file_int { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::CRC32->new->addfile(@_)->intdigest } | |
34 | 18 | |
35 | 19 | 1; |
36 | 20 | |
42 | 26 | |
43 | 27 | =head1 SYNOPSIS |
44 | 28 | |
29 | ### Functional interface: | |
30 | use Crypt::Checksum::CRC32 ':all'; | |
31 | ||
32 | # calculate CRC32 checksum from string/buffer | |
33 | $checksum_raw = crc32_data($data); | |
34 | $checksum_hex = crc32_data_hex($data); | |
35 | $checksum_int = crc32_data_int($data); | |
36 | # calculate CRC32 checksum from file | |
37 | $checksum_raw = crc32_file('filename.dat'); | |
38 | $checksum_hex = crc32_file_hex('filename.dat'); | |
39 | $checksum_int = crc32_file_int('filename.dat'); | |
40 | # calculate CRC32 checksum from filehandle | |
41 | $checksum_raw = crc32_file(*FILEHANDLE); | |
42 | $checksum_hex = crc32_file_hex(*FILEHANDLE); | |
43 | $checksum_int = crc32_file_int(*FILEHANDLE); | |
44 | ||
45 | ### OO interface: | |
45 | 46 | use Crypt::Checksum::CRC32; |
46 | 47 | |
47 | 48 | $d = Crypt::Checksum::CRC32->new; |
48 | 49 | $d->add('any data'); |
50 | $d->add('another data'); | |
49 | 51 | $d->addfile('filename.dat'); |
50 | 52 | $d->addfile(*FILEHANDLE); |
51 | $checksum_raw = $d->digest; # raw bytes | |
53 | $checksum_raw = $d->digest; # raw 4 bytes | |
52 | 54 | $checksum_hex = $d->hexdigest; # hexadecimal form |
55 | $checksum_int = $d->intdigest; # 32bit unsigned integer | |
53 | 56 | |
54 | 57 | =head1 DESCRIPTION |
55 | 58 | |
56 | Calculating CRC32 checksums (OO interface); | |
59 | Calculating CRC32 checksums. | |
57 | 60 | |
58 | I<Since: CryptX-0.032> | |
61 | I<Updated: v0.057> | |
62 | ||
63 | =head1 EXPORT | |
64 | ||
65 | Nothing is exported by default. | |
66 | ||
67 | You can export selected functions: | |
68 | ||
69 | use Crypt::Checksum::CRC32 qw(crc32_data crc32_data_hex crc32_data_int crc32_file crc32_file_hex crc32_file_int); | |
70 | ||
71 | Or all of them at once: | |
72 | ||
73 | use Crypt::Checksum::CRC32 ':all'; | |
74 | ||
75 | =head1 FUNCTIONS | |
76 | ||
77 | =head2 crc32_data | |
78 | ||
79 | Returns checksum as raw octects. | |
80 | ||
81 | $checksum_raw = crc32_data('data string'); | |
82 | #or | |
83 | $checksum_raw = crc32_data('any data', 'more data', 'even more data'); | |
84 | ||
85 | =head2 crc32_data_hex | |
86 | ||
87 | Returns checksum as a hexadecimal string. | |
88 | ||
89 | $checksum_hex = crc32_data_hex('data string'); | |
90 | #or | |
91 | $checksum_hex = crc32_data_hex('any data', 'more data', 'even more data'); | |
92 | ||
93 | =head2 crc32_data_int | |
94 | ||
95 | Returns checksum as unsigned 32bit integer. | |
96 | ||
97 | $checksum_hex = crc32_data_int('data string'); | |
98 | #or | |
99 | $checksum_hex = crc32_data_int('any data', 'more data', 'even more data'); | |
100 | ||
101 | =head2 crc32_file | |
102 | ||
103 | Returns checksum as raw octects. | |
104 | ||
105 | $checksum_raw = crc32_file('filename.dat'); | |
106 | #or | |
107 | $checksum_raw = crc32_file(*FILEHANDLE); | |
108 | ||
109 | =head2 crc32_file_hex | |
110 | ||
111 | Returns checksum as a hexadecimal string. | |
112 | ||
113 | $checksum_hex = crc32_file_hex('filename.dat'); | |
114 | #or | |
115 | $checksum_hex = crc32_file_hex(*FILEHANDLE); | |
116 | ||
117 | =head2 crc32_file_int | |
118 | ||
119 | Returns checksum as unsigned 32bit integer. | |
120 | ||
121 | $checksum_hex = crc32_file_int('filename.dat'); | |
122 | #or | |
123 | $checksum_hex = crc32_file_int(*FILEHANDLE); | |
59 | 124 | |
60 | 125 | =head1 METHODS |
61 | 126 | |
109 | 174 | |
110 | 175 | $result_hex = $d->hexdigest(); |
111 | 176 | |
177 | =head2 intdigest | |
178 | ||
179 | Returns the checksum encoded as unsigned 32bit integer. | |
180 | ||
181 | $result_int = $d->intdigest(); | |
182 | ||
112 | 183 | =head1 SEE ALSO |
113 | 184 | |
114 | 185 | =over |
115 | 186 | |
116 | =item * L<CryptX|CryptX>, L<Crypt::Checksum> | |
187 | =item * L<CryptX|CryptX> | |
117 | 188 | |
118 | 189 | =item * L<https://en.wikipedia.org/wiki/Cyclic_redundancy_check> |
119 | 190 |
4 | 4 | our $VERSION = '0.056_006'; |
5 | 5 | |
6 | 6 | require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import'; |
7 | our %EXPORT_TAGS = ( all => [qw/ | |
8 | adler32_data adler32_data_hex adler32_data_int adler32_file adler32_file_hex adler32_file_int | |
9 | crc32_data crc32_data_hex crc32_data_int crc32_file crc32_file_hex crc32_file_int | |
10 | /] ); | |
7 | our %EXPORT_TAGS = ( all => [qw/ adler32_data adler32_data_hex adler32_data_int adler32_file adler32_file_hex adler32_file_int | |
8 | crc32_data crc32_data_hex crc32_data_int crc32_file crc32_file_hex crc32_file_int /] ); | |
11 | 9 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
12 | 10 | our @EXPORT = qw(); |
13 | 11 | |
14 | 12 | use Carp; |
15 | 13 | $Carp::Internal{(__PACKAGE__)}++; |
16 | use CryptX; | |
14 | ||
15 | # obsolete since v0.057, only for backwards compatibility | |
16 | use Crypt::Checksum::CRC32; | |
17 | 17 | use Crypt::Checksum::Adler32; |
18 | use Crypt::Checksum::CRC32; | |
18 | sub adler32_data { goto \&Crypt::Checksum::Adler32::adler32_data } | |
19 | sub adler32_data_hex { goto \&Crypt::Checksum::Adler32::adler32_data_hex } | |
20 | sub adler32_data_int { goto \&Crypt::Checksum::Adler32::adler32_data_int } | |
21 | sub adler32_file { goto \&Crypt::Checksum::Adler32::adler32_file } | |
22 | sub adler32_file_hex { goto \&Crypt::Checksum::Adler32::adler32_file_hex } | |
23 | sub adler32_file_int { goto \&Crypt::Checksum::Adler32::adler32_file_int } | |
24 | sub crc32_data { goto \&Crypt::Checksum::CRC32::crc32_data } | |
25 | sub crc32_data_hex { goto \&Crypt::Checksum::CRC32::crc32_data_hex } | |
26 | sub crc32_data_int { goto \&Crypt::Checksum::CRC32::crc32_data_int } | |
27 | sub crc32_file { goto \&Crypt::Checksum::CRC32::crc32_file } | |
28 | sub crc32_file_hex { goto \&Crypt::Checksum::CRC32::crc32_file_hex } | |
29 | sub crc32_file_int { goto \&Crypt::Checksum::CRC32::crc32_file_int } | |
19 | 30 | |
20 | sub adler32_data { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::Adler32->new->add(@_)->digest } | |
21 | sub adler32_data_hex { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::Adler32->new->add(@_)->hexdigest } | |
22 | sub adler32_data_int { local $SIG{__DIE__} = \&CryptX::_croak; unpack("N", Crypt::Checksum::Adler32->new->add(@_)->digest) } | |
23 | sub adler32_file { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::Adler32->new->addfile(@_)->digest } | |
24 | sub adler32_file_hex { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::Adler32->new->addfile(@_)->hexdigest } | |
25 | sub adler32_file_int { local $SIG{__DIE__} = \&CryptX::_croak; unpack("N", Crypt::Checksum::Adler32->new->addfile(@_)->digest) } | |
26 | sub crc32_data { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::CRC32->new->add(@_)->digest } | |
27 | sub crc32_data_hex { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::CRC32->new->add(@_)->hexdigest } | |
28 | sub crc32_data_int { local $SIG{__DIE__} = \&CryptX::_croak; unpack("N", Crypt::Checksum::CRC32->new->add(@_)->digest) } | |
29 | sub crc32_file { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::CRC32->new->addfile(@_)->digest } | |
30 | sub crc32_file_hex { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::CRC32->new->addfile(@_)->hexdigest } | |
31 | sub crc32_file_int { local $SIG{__DIE__} = \&CryptX::_croak; unpack("N", Crypt::Checksum::CRC32->new->addfile(@_)->digest) } | |
31 | sub addfile { | |
32 | my ($self, $file) = @_; | |
33 | ||
34 | my $handle; | |
35 | if (ref(\$file) eq 'SCALAR') { #filename | |
36 | open($handle, "<", $file) || croak "FATAL: cannot open '$file': $!"; | |
37 | binmode($handle); | |
38 | } | |
39 | else { #handle | |
40 | $handle = $file | |
41 | } | |
42 | croak "FATAL: invalid handle" unless defined $handle; | |
43 | ||
44 | my $n; | |
45 | my $buf = ""; | |
46 | while (($n = read($handle, $buf, 32*1024))) { | |
47 | $self->add($buf) | |
48 | } | |
49 | croak "FATAL: read failed: $!" unless defined $n; | |
50 | ||
51 | return $self; | |
52 | } | |
53 | ||
54 | sub CLONE_SKIP { 1 } # prevent cloning | |
32 | 55 | |
33 | 56 | 1; |
34 | 57 | |
36 | 59 | |
37 | 60 | =head1 NAME |
38 | 61 | |
39 | Crypt::Checksum - functional interface to CRC32 and Adler32 checksums | |
40 | ||
41 | =head1 SYNOPSIS | |
42 | ||
43 | use Crypt::Checksum ':all'; | |
44 | ||
45 | # calculate Adler32 checksum from string/buffer | |
46 | $checksum_raw = adler32_data($data); | |
47 | $checksum_hex = adler32_data_hex($data); | |
48 | ||
49 | # calculate Adler32 checksum from file | |
50 | $checksum_raw = adler32_file('filename.dat'); | |
51 | $checksum_hex = adler32_file_hex('filename.dat'); | |
52 | ||
53 | # calculate Adler32 checksum from filehandle | |
54 | $checksum_raw = adler32_file(*FILEHANDLE); | |
55 | $checksum_hex = adler32_file_hex(*FILEHANDLE); | |
56 | ||
57 | # calculate CRC32 checksum from string/buffer | |
58 | $checksum_raw = crc32_data($data); | |
59 | $checksum_hex = crc32_data_hex($data); | |
60 | ||
61 | # calculate CRC32 checksum from file | |
62 | $checksum_raw = crc32_file('filename.dat'); | |
63 | $checksum_hex = crc32_file_hex('filename.dat'); | |
64 | ||
65 | # calculate CRC32 checksum from filehandle | |
66 | $checksum_raw = crc32_file(*FILEHANDLE); | |
67 | $checksum_hex = crc32_file_hex(*FILEHANDLE); | |
62 | Crypt::Checksum - [internal only] | |
68 | 63 | |
69 | 64 | =head1 DESCRIPTION |
70 | 65 | |
71 | Calculating CRC32 and Adler32 checksums (functional interface); | |
72 | ||
73 | I<Since: CryptX-0.032> | |
74 | ||
75 | =head1 EXPORT | |
76 | ||
77 | Nothing is exported by default. | |
78 | ||
79 | You can export selected functions: | |
80 | ||
81 | use Crypt::Checksum qw( adler32_data adler32_data_hex adler32_file adler32_file_hex | |
82 | crc32_data crc32_data_hex crc32_file crc32_file_hex ); | |
83 | ||
84 | Or all of them at once: | |
85 | ||
86 | use Crypt::Checksum ':all'; | |
87 | ||
88 | =head1 FUNCTIONS | |
89 | ||
90 | =head2 adler32_data | |
91 | ||
92 | Returns checksum as raw octects. | |
93 | ||
94 | $checksum_raw = adler32_data('data string'); | |
95 | #or | |
96 | $checksum_raw = adler32_data('any data', 'more data', 'even more data'); | |
97 | ||
98 | =head2 adler32_data_hex | |
99 | ||
100 | Returns checksum as a hexadecimal string. | |
101 | ||
102 | $checksum_hex = adler32_data_hex('data string'); | |
103 | #or | |
104 | $checksum_hex = adler32_data_hex('any data', 'more data', 'even more data'); | |
105 | ||
106 | =head2 adler32_data_int | |
107 | ||
108 | Returns checksum as unsigned 32bit integer. | |
109 | ||
110 | $checksum_hex = adler32_data_int('data string'); | |
111 | #or | |
112 | $checksum_hex = adler32_data_int('any data', 'more data', 'even more data'); | |
113 | ||
114 | =head2 adler32_file | |
115 | ||
116 | Returns checksum as raw octects. | |
117 | ||
118 | $checksum_raw = adler32_file('filename.dat'); | |
119 | #or | |
120 | $checksum_raw = adler32_file(*FILEHANDLE); | |
121 | ||
122 | =head2 adler32_file_hex | |
123 | ||
124 | Returns checksum as a hexadecimal string. | |
125 | ||
126 | $checksum_hex = adler32_file_hex('filename.dat'); | |
127 | #or | |
128 | $checksum_hex = adler32_file_hex(*FILEHANDLE); | |
129 | ||
130 | =head2 adler32_file_int | |
131 | ||
132 | Returns checksum as unsigned 32bit integer. | |
133 | ||
134 | $checksum_hex = adler32_file_int('filename.dat'); | |
135 | #or | |
136 | $checksum_hex = adler32_file_int(*FILEHANDLE); | |
137 | ||
138 | =head2 crc32_data | |
139 | ||
140 | Returns checksum as raw octects. | |
141 | ||
142 | $checksum_raw = crc32_data('data string'); | |
143 | #or | |
144 | $checksum_raw = crc32_data('any data', 'more data', 'even more data'); | |
145 | ||
146 | =head2 crc32_data_hex | |
147 | ||
148 | Returns checksum as a hexadecimal string. | |
149 | ||
150 | $checksum_hex = crc32_data_hex('data string'); | |
151 | #or | |
152 | $checksum_hex = crc32_data_hex('any data', 'more data', 'even more data'); | |
153 | ||
154 | =head2 crc32_data_int | |
155 | ||
156 | Returns checksum as unsigned 32bit integer. | |
157 | ||
158 | $checksum_hex = crc32_data_int('data string'); | |
159 | #or | |
160 | $checksum_hex = crc32_data_int('any data', 'more data', 'even more data'); | |
161 | ||
162 | =head2 crc32_file | |
163 | ||
164 | Returns checksum as raw octects. | |
165 | ||
166 | $checksum_raw = crc32_file('filename.dat'); | |
167 | #or | |
168 | $checksum_raw = crc32_file(*FILEHANDLE); | |
169 | ||
170 | =head2 crc32_file_hex | |
171 | ||
172 | Returns checksum as a hexadecimal string. | |
173 | ||
174 | $checksum_hex = crc32_file_hex('filename.dat'); | |
175 | #or | |
176 | $checksum_hex = crc32_file_hex(*FILEHANDLE); | |
177 | ||
178 | =head2 crc32_file_int | |
179 | ||
180 | Returns checksum as unsigned 32bit integer. | |
181 | ||
182 | $checksum_hex = crc32_file_int('filename.dat'); | |
183 | #or | |
184 | $checksum_hex = crc32_file_int(*FILEHANDLE); | |
185 | ||
186 | =head1 SEE ALSO | |
187 | ||
188 | =over | |
189 | ||
190 | =item * L<CryptX|CryptX>, L<Crypt::Checksum::Adler32>, L<Crypt::Checksum::CRC32> | |
191 | ||
192 | =item * L<https://en.wikipedia.org/wiki/Adler-32> | |
193 | ||
194 | =item * L<https://en.wikipedia.org/wiki/Cyclic_redundancy_check> | |
195 | ||
196 | =back | |
66 | You are probably looking for L<Crypt::Checksum::CRC32> or L<Crypt::Checksum::Adler32>. | |
197 | 67 | |
198 | 68 | =cut |
9 | 9 | our @EXPORT = qw(); |
10 | 10 | |
11 | 11 | use CryptX; |
12 | use base 'Crypt::PRNG'; | |
13 | 12 | |
14 | 13 | { |
15 | 14 | ### stolen from Bytes::Random::Secure |
9 | 9 | our @EXPORT = qw(); |
10 | 10 | |
11 | 11 | use CryptX; |
12 | use base 'Crypt::PRNG'; | |
13 | 12 | |
14 | 13 | { |
15 | 14 | ### stolen from Bytes::Random::Secure |
9 | 9 | our @EXPORT = qw(); |
10 | 10 | |
11 | 11 | use CryptX; |
12 | use base 'Crypt::PRNG'; | |
13 | 12 | |
14 | 13 | { |
15 | 14 | ### stolen from Bytes::Random::Secure |
9 | 9 | our @EXPORT = qw(); |
10 | 10 | |
11 | 11 | use CryptX; |
12 | use base 'Crypt::PRNG'; | |
13 | 12 | |
14 | 13 | { |
15 | 14 | ### stolen from Bytes::Random::Secure |
2 | 2 | use strict; |
3 | 3 | use warnings ; |
4 | 4 | our $VERSION = '0.056_006'; |
5 | ||
6 | use base qw(Exporter); | |
7 | our @EXPORT_OK = qw(); | |
8 | 5 | |
9 | 6 | require XSLoader; |
10 | 7 | XSLoader::load('CryptX', $VERSION); |
27 | 27 | elsif ($m eq 'Crypt::Mode') { |
28 | 28 | $pc = Pod::Coverage->new(package => $m, pod_from => $f, trustme => [qr/^(add|decrypt|encrypt|finish|new|start_decrypt|start_encrypt)$/] ); |
29 | 29 | } |
30 | elsif ($m eq 'Crypt::Checksum') { | |
31 | $pc = Pod::Coverage->new(package => $m, pod_from => $f, trustme => [qr/^(addfile|(adler32_|crc32_)(file_hex|file_int|file|data_hex|data_int|data))$/] ); | |
32 | } | |
30 | 33 | elsif ($m eq 'Crypt::Mac') { |
31 | 34 | $pc = Pod::Coverage->new(package => $m, pod_from => $f, trustme => [qr/^(add|addfile)$/] ); |
32 | 35 | } |
0 | 0 | use strict; |
1 | 1 | use warnings; |
2 | 2 | |
3 | use Test::More tests => 24; | |
3 | use Test::More tests => 54; | |
4 | 4 | |
5 | use Crypt::Checksum ':all'; | |
6 | use Crypt::Checksum::Adler32; | |
7 | use Crypt::Checksum::CRC32; | |
5 | use Crypt::Checksum::Adler32 ':all'; | |
6 | use Crypt::Checksum::CRC32 ':all'; | |
8 | 7 | |
9 | my $a32 = Crypt::Checksum::Adler32->new; | |
10 | is($a32->hexdigest, "00000001"); | |
11 | is($a32->hexdigest, "00000001"); | |
12 | $a32->add("a"); | |
13 | is($a32->hexdigest, "00620062"); | |
14 | $a32->reset; | |
15 | is($a32->hexdigest, "00000001"); | |
16 | $a32->add("abc"); | |
17 | is($a32->hexdigest, "024d0127"); | |
18 | $a32->reset; | |
19 | $a32->add("abc"); | |
20 | $a32->add("abc"); | |
21 | is($a32->hexdigest, "080c024d"); | |
22 | $a32->reset; | |
23 | $a32->add("abcabc"); | |
24 | is($a32->hexdigest, "080c024d"); | |
25 | $a32->reset; | |
26 | $a32->add("\xFF" x 32); | |
27 | is($a32->hexdigest, "0e2e1fe1"); | |
28 | is(adler32_data_hex("a"), "00620062"); | |
29 | is(adler32_data("a"), pack("H*","00620062")); | |
8 | { | |
9 | my $a32 = Crypt::Checksum::Adler32->new; | |
10 | is($a32->hexdigest, "00000001"); | |
11 | $a32->add("a"); | |
12 | is($a32->hexdigest, "00620062"); | |
13 | $a32->reset; | |
14 | is($a32->hexdigest, "00000001"); | |
15 | $a32->add("abc"); | |
16 | is($a32->hexdigest, "024d0127"); | |
17 | $a32->reset; | |
18 | $a32->add("abc"); | |
19 | $a32->add("abc"); | |
20 | is($a32->hexdigest, "080c024d"); | |
21 | $a32->reset; | |
22 | $a32->add("abc", "abc"); | |
23 | is($a32->hexdigest, "080c024d"); | |
24 | $a32->reset; | |
25 | $a32->add("abcabc"); | |
26 | is($a32->hexdigest, "080c024d"); | |
27 | $a32->reset; | |
28 | $a32->add("\xFF" x 32); | |
29 | is($a32->hexdigest, "0e2e1fe1"); | |
30 | is($a32->intdigest, 237903841); | |
31 | is($a32->digest, pack("H*", "0e2e1fe1")); | |
30 | 32 | |
31 | is(crc32_data_hex("a"), "e8b7be43"); | |
32 | is(crc32_data_hex("libtomcrypt"), "b37376ef"); | |
33 | is(crc32_data_hex("This is the test string"), "6d680973"); | |
34 | is(crc32_data_int("This is the test string"), 1835534707); | |
35 | is(crc32_data_hex("This is another test string"), "806e15e9"); | |
36 | is(crc32_data_int("This is another test string"), 2154698217); | |
33 | is(adler32_data_hex("aaa"), "02490124"); | |
34 | is(adler32_data_int("aaa"), 38338852); | |
35 | is(adler32_data("aaa"), pack("H*","02490124")); | |
36 | is(adler32_data_hex("a","a","a"), "02490124"); | |
37 | is(adler32_data_int("a","a","a"), 38338852); | |
38 | is(adler32_data("a","a","a"), pack("H*","02490124")); | |
37 | 39 | |
38 | is(crc32_file_hex("t/data/binary-test.file"), "24111fed"); | |
39 | is(crc32_file_hex("t/data/text-CR.file"), "1ca430c6"); | |
40 | is(crc32_file_hex("t/data/text-CRLF.file"), "4d434dfb"); | |
41 | is(crc32_file_hex("t/data/text-LF.file"), "9f9b8258"); | |
40 | is(adler32_data_hex("libtomcrypt"), "1be804ba"); | |
41 | is(adler32_data_hex("This is the test string"), "6363088d"); | |
42 | is(adler32_data_int("This is the test string"), 1667434637); | |
43 | is(adler32_data_hex("This is another test string"), "8b900a3d"); | |
44 | is(adler32_data_int("This is another test string"), 2341472829); | |
42 | 45 | |
43 | is(adler32_file_hex("t/data/binary-test.file"), "f35fb68a"); | |
44 | is(adler32_file_hex("t/data/text-CR.file"), "948e2644"); | |
45 | is(adler32_file_hex("t/data/text-CRLF.file"), "3f0e2702"); | |
46 | is(adler32_file_hex("t/data/text-LF.file"), "86ba260b"); | |
46 | is(adler32_file("t/data/binary-test.file"), pack("H*", "f35fb68a")); | |
47 | is(adler32_file_int("t/data/binary-test.file"), 4083136138); | |
48 | is(adler32_file_hex("t/data/binary-test.file"), "f35fb68a"); | |
49 | ||
50 | is(adler32_file_hex("t/data/text-CR.file"), "948e2644"); | |
51 | is(adler32_file_hex("t/data/text-CRLF.file"), "3f0e2702"); | |
52 | is(adler32_file_hex("t/data/text-LF.file"), "86ba260b"); | |
53 | } | |
54 | ||
55 | { | |
56 | my $a32 = Crypt::Checksum::CRC32->new; | |
57 | is($a32->hexdigest, "00000000"); | |
58 | $a32->add("a"); | |
59 | is($a32->hexdigest, "e8b7be43"); | |
60 | $a32->reset; | |
61 | is($a32->hexdigest, "00000000"); | |
62 | $a32->add("abc"); | |
63 | is($a32->hexdigest, "352441c2"); | |
64 | $a32->reset; | |
65 | $a32->add("abc"); | |
66 | $a32->add("abc"); | |
67 | is($a32->hexdigest, "726e994c"); | |
68 | $a32->reset; | |
69 | $a32->add("abc", "abc"); | |
70 | is($a32->hexdigest, "726e994c"); | |
71 | $a32->reset; | |
72 | $a32->add("abcabc"); | |
73 | is($a32->hexdigest, "726e994c"); | |
74 | $a32->reset; | |
75 | $a32->add("\xFF" x 32); | |
76 | is($a32->hexdigest, "ff6cab0b"); | |
77 | is($a32->intdigest, 4285311755); | |
78 | is($a32->digest, pack("H*", "ff6cab0b")); | |
79 | ||
80 | is(crc32_data_hex("aaa"), "f007732d"); | |
81 | is(crc32_data_int("aaa"), 4027020077); | |
82 | is(crc32_data("aaa"), pack("H*","f007732d")); | |
83 | is(crc32_data_hex("a","a","a"), "f007732d"); | |
84 | is(crc32_data_int("a","a","a"), 4027020077); | |
85 | is(crc32_data("a","a","a"), pack("H*","f007732d")); | |
86 | ||
87 | is(crc32_data_hex("libtomcrypt"), "b37376ef"); | |
88 | is(crc32_data_hex("This is the test string"), "6d680973"); | |
89 | is(crc32_data_int("This is the test string"), 1835534707); | |
90 | is(crc32_data_hex("This is another test string"), "806e15e9"); | |
91 | is(crc32_data_int("This is another test string"), 2154698217); | |
92 | ||
93 | is(crc32_file("t/data/binary-test.file"), pack("H*", "24111fed")); | |
94 | is(crc32_file_int("t/data/binary-test.file"), 605102061); | |
95 | is(crc32_file_hex("t/data/binary-test.file"), "24111fed"); | |
96 | ||
97 | is(crc32_file_hex("t/data/text-CR.file"), "1ca430c6"); | |
98 | is(crc32_file_hex("t/data/text-CRLF.file"), "4d434dfb"); | |
99 | is(crc32_file_hex("t/data/text-LF.file"), "9f9b8258"); | |
100 | } |