| 1 | package AWS::Signature::V4::Checksum; | ||
| 2 | use v5.24; | ||
| 3 | use Moo; | ||
| 4 | use Compress::Raw::Zlib (); | ||
| 5 | use Digest::SHA (); | ||
| 6 | use MIME::Base64 qw< encode_base64 >; | ||
| 7 | use AWS::Signature::V4::Error qw< fail >; | ||
| 8 | use experimental qw< signatures >; | ||
| 9 | |||
| 10 | use constant { | ||
| 11 | # name => size of the raw checksum, in bytes | ||
| 12 | SIZE_FOR => +{ crc32 => 4, crc32c => 4, sha1 => 20, sha256 => 32 }, | ||
| 13 | |||
| 14 | # crc32c comes from String::CRC32C when it is installed (it is much | ||
| 15 | # faster), otherwise from the slicing-by-4 code below: $T0[$b] is the | ||
| 16 | # effect of byte $b on the register, $T1 to $T3 that of the same byte | ||
| 17 | # followed by 1 to 3 zeros | ||
| 18 | CRC32C_XS => eval {; require String::CRC32C; \&String::CRC32C::crc32c }, | ||
| 19 | }; | ||
| 20 | |||
| 21 | use namespace::clean; | ||
| 22 | |||
| 23 | has algorithm => (is => 'ro', required => 1); | ||
| 24 | |||
| 25 | # running state: the CRCs keep a number, the SHA ones a Digest::SHA object | ||
| 26 | has _crc => (is => 'rw', lazy => 1, builder => 1); | ||
| 27 | has _sha => (is => 'lazy'); | ||
| 28 | |||
| 29 | # the algorithm is given as the only, positional, argument: new('crc32') | ||
| 30 | around BUILDARGS => sub ($orig, $class, @args) { | ||
| 31 | return {algorithm => $args[0]} if @args == 1 && !ref $args[0]; | ||
| 32 | return $class->$orig(@args); | ||
| 33 | }; | ||
| 34 | |||
| 35 | sub _build__crc ($self) { $self->algorithm eq 'crc32c' ? 0xFFFFFFFF : 0 } | ||
| 36 | sub _build__sha ($self) { Digest::SHA->new($self->algorithm eq 'sha1' ? 1 : 256) } | ||
| 37 | |||
| 38 | sub encoded_size ($class, $algo) { 4 * int((SIZE_FOR->{$algo} + 2) / 3) } | ||
| 39 | |||
| 40 | sub add ($self, $ref) { # $ref: reference to a byte string | ||
| 41 | fail 400, 'checksum data must be a reference to a byte string' | ||
| 42 | unless ref $ref eq 'SCALAR' && defined $$ref; | ||
| 43 | utf8::is_utf8($$ref) && !utf8::downgrade(my $copy = $$ref, 1) | ||
| 44 | and fail 400, 'checksum data must be a byte string: encode characters first'; | ||
| 45 | my $algo = $self->algorithm; | ||
| 46 | if ($algo eq 'crc32') { | ||
| 47 | $self->_crc(Compress::Raw::Zlib::crc32($$ref, $self->_crc)); | ||
| 48 | } | ||
| 49 | elsif ($algo eq 'crc32c') { | ||
| 50 | $self->_crc(_crc32c($self->_crc, $ref)); | ||
| 51 | } | ||
| 52 | else { $self->_sha->add($$ref) } | ||
| 53 | return $self; | ||
| 54 | } | ||
| 55 | |||
| 56 | # register after the data; String::CRC32C wants and gives it inverted | ||
| 57 | sub _crc32c ($crc, $ref) { | ||
| 58 | return CRC32C_XS->($$ref, $crc ^ 0xFFFFFFFF) ^ 0xFFFFFFFF if CRC32C_XS; | ||
| 59 | |||
| 60 | state $T0 = [ | ||
| 61 | map { | ||
| 62 | my $c = $_; | ||
| 63 | $c = ($c & 1) ? (($c >> 1) ^ 0x82F63B78) : ($c >> 1) for 1 .. 8; | ||
| 64 | $c; | ||
| 65 | } 0 .. 255 | ||
| 66 | ]; | ||
| 67 | state $T1 = [ map { $T0->[$T0->[$_] & 0xFF] ^ ($T0->[$_] >> 8) } 0 .. 255 ]; | ||
| 68 | state $T2 = [ map { $T0->[$T1->[$_] & 0xFF] ^ ($T1->[$_] >> 8) } 0 .. 255 ]; | ||
| 69 | state $T3 = [ map { $T0->[$T2->[$_] & 0xFF] ^ ($T2->[$_] >> 8) } 0 .. 255 ]; | ||
| 70 | |||
| 71 | |||
| 72 | my $words = length($$ref) & ~3; | ||
| 73 | for (my $offset = 0; $offset < $words; $offset += 65536) { # bounded lists | ||
| 74 | my $size = $words - $offset < 65536 ? $words - $offset : 65536; | ||
| 75 | for my $word (unpack 'V*', substr $$ref, $offset, $size) { | ||
| 76 | my $c = $crc ^ $word; | ||
| 77 | $crc = $T3->[$c & 0xFF] | ||
| 78 | ^ $T2->[($c >> 8) & 0xFF] | ||
| 79 | ^ $T1->[($c >> 16) & 0xFF] | ||
| 80 | ^ $T0->[$c >> 24]; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | $crc = $T0->[($crc ^ $_) & 0xFF] ^ ($crc >> 8) for unpack 'C*', substr $$ref, $words; | ||
| 84 | return $crc; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub base64 ($self) { | ||
| 88 | my $algo = $self->algorithm; | ||
| 89 | my $raw = | ||
| 90 | $algo eq 'crc32' ? pack('N', $self->_crc) | ||
| 91 | : $algo eq 'crc32c' ? pack('N', $self->_crc ^ 0xFFFFFFFF) | ||
| 92 | : $self->_sha->digest; | ||
| 93 | return encode_base64($raw, ''); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub is_supported_algorithm ($co, $name) { exists(SIZE_FOR->{$name}) } | ||
| 97 | |||
| 98 | sub supported_algorithms { sort { $a cmp $b } keys(SIZE_FOR->%*) } | ||
| 99 | |||
| 100 | 1; |