#include "highwayhash.c"
+typedef HighwayHashCat* Digest__HighwayHash;
+
void process_key(pTHX_ AV *key_av, uint64_t *key) {
int i;
SV *elt;
}
}
-uint64_t highway_hash64(AV *key_av, unsigned char *bytes, uint64_t size) {
+uint64_t highway_hash64(AV *key_av, unsigned char *bytes, size_t size) {
dTHX;
uint64_t key[4];
process_key(aTHX_ key_av, key);
return HighwayHash64(bytes, size, key);
}
-AV* highway_hash128(AV *key_av, unsigned char *bytes, uint64_t size) {
+AV* highway_hash128(AV *key_av, unsigned char *bytes, size_t size) {
dTHX;
AV* result;
uint64_t key[4];
return result;
}
-AV* highway_hash256(AV *key_av, unsigned char *bytes, uint64_t size) {
+AV* highway_hash256(AV *key_av, unsigned char *bytes, size_t size) {
dTHX;
AV* result;
uint64_t key[4];
return result;
}
+Digest__HighwayHash new(AV *key_av) {
+ dTHX;
+ uint64_t key[4];
+ Digest__HighwayHash state;
+ Newx(state, 1, HighwayHashCat);
+ process_key(aTHX_ key_av, key);
+ HighwayHashCatStart(key, state);
+ return state;
+}
+
+void append(Digest__HighwayHash state, unsigned char *bytes, size_t size) {
+ HighwayHashCatAppend(bytes, size, state);
+}
+
+uint64_t finish64(Digest__HighwayHash state) {
+ return HighwayHashCatFinish64(state);
+}
+
+AV* finish128(Digest__HighwayHash state) {
+ dTHX;
+ AV* result;
+ uint64_t hash[2];
+ HighwayHashCatFinish128(state, hash);
+ result = newAV();
+ av_push(result, sv_2mortal(newSVu64(hash[0])));
+ av_push(result, sv_2mortal(newSVu64(hash[1])));
+ return result;
+}
+
+AV* finish256(Digest__HighwayHash state) {
+ dTHX;
+ AV* result;
+ uint64_t hash[4];
+ HighwayHashCatFinish256(state, hash);
+ result = newAV();
+ av_push(result, sv_2mortal(newSVu64(hash[0])));
+ av_push(result, sv_2mortal(newSVu64(hash[1])));
+ av_push(result, sv_2mortal(newSVu64(hash[2])));
+ av_push(result, sv_2mortal(newSVu64(hash[3])));
+ return result;
+}
+
MODULE = Digest::HighwayHash PACKAGE = Digest::HighwayHash
PROTOTYPES: DISABLE
BOOT:
PERL_MATH_INT64_LOAD_OR_CROAK;
-uint64_t highway_hash64(AV *key_av, unsigned char *bytes, uint64_t length(bytes))
+uint64_t highway_hash64(AV *key_av, unsigned char *bytes, size_t length(bytes))
+
+AV* highway_hash128(AV *key_av, unsigned char *bytes, size_t length(bytes))
+
+AV* highway_hash256(AV *key_av, unsigned char *bytes, size_t length(bytes))
+
+Digest::HighwayHash new(SV *class, AV *key_av)
+C_ARGS: key_av
+
+void append(Digest::HighwayHash state, unsigned char *bytes, size_t length(bytes))
+
+uint64_t finish64(Digest::HighwayHash state)
-AV* highway_hash128(AV *key_av, unsigned char *bytes, uint64_t length(bytes))
+AV* finish128(Digest::HighwayHash state)
-AV* highway_hash256(AV *key_av, unsigned char *bytes, uint64_t length(bytes))
+AV* finish256(Digest::HighwayHash state)
use strict;
use warnings;
-use Test::More tests => 7;
+use Test::More tests => 10;
use Test::Exception;
BEGIN { use_ok('Digest::HighwayHash') };
is_deeply highway_hash128([1, 2, 3, 4], 'hello'), ['3048112761216189476', '13900443277579286659'], 'highway_hash128';
is_deeply highway_hash256([1, 2, 3, 4], 'hello'), ['8099666330974151427', '17027479935588128037', '4015249936799013189', '10027181291351549853'], 'highway_hash256';
+my $state64 = Digest::HighwayHash->new([1, 2, 3, 4]);
+my $state128 = Digest::HighwayHash->new([1, 2, 3, 4]);
+my $state256 = Digest::HighwayHash->new([1, 2, 3, 4]);
+
+$state64->append('hello');
+$state64->append('');
+$state128->append('h');
+$state128->append('ell');
+$state128->append('o');
+$state256->append('hell');
+$state256->append('o');
+
+is $state64->finish64, '11956820856122239241', 'finish64';
+is_deeply $state128->finish128, ['3048112761216189476', '13900443277579286659'], 'finish128';
+is_deeply $state256->finish256, ['8099666330974151427', '17027479935588128037', '4015249936799013189', '10027181291351549853'], 'finish256';
+
throws_ok { highway_hash64 1, 'hello' } qr/not an ARRAY reference/, 'bad key 1';
throws_ok { highway_hash128 [1, 2], 'hello' } qr/Key for highway_hash must be a 4-element array/, 'bad key 2';
throws_ok { highway_hash256 [1, 2, 3, 4, 5], 'hello' } qr/Key for highway_hash must be a 4-element array/, 'bad key 3';