? sha1bench.patch ? config/semantic.cache ? src/utils/lib/gettext.ml ? src/utils/lib/semantic.cache ? src/utils/lib/sha1v1.c ? src/utils/lib/sha1v1.h ? tools/sha1bench.ml Index: config/Makefile.in =================================================================== RCS file: /cvsroot/mldonkey/mldonkey/config/Makefile.in,v retrieving revision 1.64 diff -u -w -r1.64 Makefile.in --- config/Makefile.in 4 Feb 2004 00:21:44 -0000 1.64 +++ config/Makefile.in 4 Feb 2004 18:32:29 -0000 @@ -149,7 +149,7 @@ $(LIB)/avifile.ml $(LIB)/http_lexer.mll $(LIB)/url.ml \ $(LIB)/date.ml $(LIB)/fst_hash.c \ $(LIB)/md4_comp.c $(LIB)/md4_c.c \ - $(LIB)/gettext.ml4 $(LIB)/md5_c.c $(LIB)/sha1_c.c \ + $(LIB)/gettext.ml4 $(LIB)/md5_c.c $(LIB)/sha1_c.c $(LIB)/sha1v1.c \ $(LIB)/tiger.c \ $(LIB)/stubs_c.c $(LIB)/queues.ml @@ -289,6 +289,11 @@ $(CHAT_SRCS) \ tools/ed2k_hash.ml +SHA1BENCH_SRCS = \ + $(CDK_SRCS) $(LIB_SRCS) $(NET_SRCS) $(MP3TAG_SRCS) \ + $(CHAT_SRCS) \ + tools/sha1bench.ml + MLPIC_SRCS= \ $(CDK_SRCS) $(LIB_SRCS) $(NET_SRCS) \ src/pic/picTypes.ml \ @@ -1054,6 +1059,7 @@ EXPAND(MLCHAT,mlchat,GTK,MLCHAT) EXPAND(OBSERVER,observer) EXPAND(ED2K_HASH,ed2k_hash) +EXPAND(SHA1BENCH,sha1bench) EXPAND(MAKE_TORRENT,make_torrent) EXPAND(COPYSOURCES,copysources) EXPAND(USE_TAGS,use_tags,NO,USE_TAGS) Index: src/utils/lib/md4.ml =================================================================== RCS file: /cvsroot/mldonkey/mldonkey/src/utils/lib/md4.ml,v retrieving revision 1.10 diff -u -w -r1.10 md4.ml --- src/utils/lib/md4.ml 3 Dec 2003 13:35:39 -0000 1.10 +++ src/utils/lib/md4.ml 4 Feb 2004 18:33:27 -0000 @@ -448,6 +448,52 @@ lprintf "(used only if you run the BitTorrent plugin)\n"; false end + + +module PreSha1v1 = Make(struct + let hash_length = 20 + let hash_name = "Sha1v1" + + external unsafe_string : string -> string -> int -> unit = "sha1v1_unsafe_string" + external unsafe_file : string -> string -> int64 -> unit = "sha1v1_unsafe_file" + external digest_subfile : string -> Unix.file_descr -> int64 -> int64 -> unit = + "sha1v1_unsafe64_fd" + + module Base = Base32 + + end) + + +module Sha1v1 = struct + include PreSha1v1 + open PreSha1v1 + open Test + open Printf2 + + let enabled = + try + let sha1 = "ABCDEFGHGHIJKLMNOPQRSTUVWXYZ2ABC" in + assert (to_string (of_string sha1) = sha1); + + assert (to_string (string s1) = + "3I42H3S6NNFQ2MSVX7XZKYAYSCX5QBYJ"); + assert (to_string (string s2) = + "LOUTZHNQZ74T6UVVEHLUEDSD63W2E6CP"); + assert (to_string (string s3) = + "ORWD6TJINRJR4BS6RL3W4CWAQ2EDDRVU"); + assert (to_string (string s4) = + "UUHHSQPHQXN5X6EMYK6CD7IJ7BHZTE77"); + + true + with e -> + lprintf "Unable to compute correct Sha1 hashes.\n"; + lprintf "Send a bug report with your configuration\n"; + lprintf "and how you obtained this executable.\n"; + lprintf "Running with Sha1 tree corruption detection disabled.\n"; + lprintf "(used only if you run the BitTorrent plugin)\n"; + false + end + module Tiger = Make(struct let hash_length = 24 Index: src/utils/lib/md4.mli =================================================================== RCS file: /cvsroot/mldonkey/mldonkey/src/utils/lib/md4.mli,v retrieving revision 1.8 diff -u -w -r1.8 md4.mli --- src/utils/lib/md4.mli 30 Nov 2003 20:22:24 -0000 1.8 +++ src/utils/lib/md4.mli 4 Feb 2004 18:33:27 -0000 @@ -71,6 +71,7 @@ module Md4 : Digest module Md5 : Digest module Sha1 : Digest +module Sha1v1 : Digest module TigerTree : Digest module Tiger : Digest module Md5Ext : Digest Index: src/utils/lib/stubs_c.c =================================================================== RCS file: /cvsroot/mldonkey/mldonkey/src/utils/lib/stubs_c.c,v retrieving revision 1.9 diff -u -w -r1.9 stubs_c.c --- src/utils/lib/stubs_c.c 30 Jan 2004 06:11:07 -0000 1.9 +++ src/utils/lib/stubs_c.c 4 Feb 2004 18:33:27 -0000 @@ -558,7 +558,9 @@ #include "md4.h" #include "md5.h" #include "sha1_c.h" +#include "sha1v1.h" +ML_HASH(sha1v1,SHA1V1_CTX,sha1v1_begin,sha1v1_hash, sha1v1_end) ML_HASH(sha1,SHA1_CTX,sha1_init,sha1_append, sha1_finish) ML_HASH(md5,md5_state_t,md5_init,md5_append,md5_finish) ML_HASH(md4,MD4_CTX,MD4Init,MD4Update,md4_finish) --- /dev/null 2003-04-09 14:48:48.000000000 +0200 +++ src/utils/lib/sha1v1.c 2004-02-04 18:18:07.000000000 +0100 @@ -0,0 +1,355 @@ +/* + --------------------------------------------------------------------------- + Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved. + + LICENSE TERMS + + The free distribution and use of this software in both source and binary + form is allowed (with or without changes) provided that: + + 1. distributions of this source code include the above copyright + notice, this list of conditions and the following disclaimer; + + 2. distributions in binary form include the above copyright + notice, this list of conditions and the following disclaimer + in the documentation and/or other associated materials; + + 3. the copyright holder's name is not used to endorse products + built using this software without specific written permission. + + ALTERNATIVELY, provided that this notice is retained in full, this product + may be distributed under the terms of the GNU General Public License (GPL), + in which case the provisions of the GPL apply INSTEAD OF those given above. + + DISCLAIMER + + This software is provided 'as is' with no explicit or implied warranties + in respect of its properties, including, but not limited to, correctness + and/or fitness for purpose. + --------------------------------------------------------------------------- + Issue Date: 16/01/2004 + + This is a byte oriented version of SHA1 that operates on arrays of bytes + stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor +*/ + +#include /* for memcpy() etc. */ +#include /* for _lrotl with VC++ */ + +#include "sha1v1.h" + +#if defined(__cplusplus) +extern "C" +{ +#endif + +/* + To obtain the highest speed on processors with 32-bit words, this code + needs to determine the order in which bytes are packed into such words. + The following block of code is an attempt to capture the most obvious + ways in which various environemnts specify their endian definitions. + It may well fail, in which case the definitions will need to be set by + editing at the points marked **** EDIT HERE IF NECESSARY **** below. +*/ + +/* PLATFORM SPECIFIC INCLUDES */ + +#define BRG_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */ +#define BRG_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */ + +#if defined(__GNUC__) || defined(__GNU_LIBRARY__) +# if defined(__FreeBSD__) || defined(__OpenBSD__) +# include +# elif defined( BSD ) && ( BSD >= 199103 ) +# include +# elif defined(__APPLE__) +# if defined(__BIG_ENDIAN__) && !defined( BIG_ENDIAN ) +# define BIG_ENDIAN +# elif defined(__LITTLE_ENDIAN__) && !defined( LITTLE_ENDIAN ) +# define LITTLE_ENDIAN +# endif +# else +# include +# if !defined(__BEOS__) +# include +# endif +# endif +#endif + +#if !defined(PLATFORM_BYTE_ORDER) +# if defined(LITTLE_ENDIAN) || defined(BIG_ENDIAN) +# if defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) +# define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN +# elif !defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) +# define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN +# elif defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN) +# define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN +# elif defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN) +# define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN +# endif +# elif defined(_LITTLE_ENDIAN) || defined(_BIG_ENDIAN) +# if defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN) +# define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN +# elif !defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN) +# define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN +# elif defined(_BYTE_ORDER) && (_BYTE_ORDER == _LITTLE_ENDIAN) +# define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN +# elif defined(_BYTE_ORDER) && (_BYTE_ORDER == _BIG_ENDIAN) +# define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN +# endif +# elif defined(__LITTLE_ENDIAN__) || defined(__BIG_ENDIAN__) +# if defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__) +# define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN +# elif !defined(__LITTLE_ENDIAN__) && defined(__BIG_ENDIAN__) +# define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN +# elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __LITTLE_ENDIAN__) +# define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN +# elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __BIG_ENDIAN__) +# define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN +# endif +# endif +#endif + +/* if the platform is still unknown, try to find its byte order */ +/* from commonly used machine defines */ + +#if !defined(PLATFORM_BYTE_ORDER) + +#if defined( __alpha__ ) || defined( __alpha ) || defined( i386 ) || \ + defined( __i386__ ) || defined( _M_I86 ) || defined( _M_IX86 ) || \ + defined( __OS2__ ) || defined( sun386 ) || defined( __TURBOC__ ) || \ + defined( vax ) || defined( vms ) || defined( VMS ) || \ + defined( __VMS ) +# define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN + +#elif defined( AMIGA ) || defined( applec ) || defined( __AS400__ ) || \ + defined( _CRAY ) || defined( __hppa ) || defined( __hp9000 ) || \ + defined( ibm370 ) || defined( mc68000 ) || defined( m68k ) || \ + defined( __MRC__ ) || defined( __MVS__ ) || defined( __MWERKS__ ) || \ + defined( sparc ) || defined( __sparc) || defined( SYMANTEC_C ) || \ + defined( __TANDEM ) || defined( THINK_C ) || defined( __VMCMS__ ) +# define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN + +#elif 0 /* **** EDIT HERE IF NECESSARY **** */ +# define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN +#elif 0 /* **** EDIT HERE IF NECESSARY **** */ +# define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN +#else +# error Please edit sha1v1.c (line 134 or 136) to set the platform byte order +#endif + +#endif + +#ifdef _MSC_VER +#pragma intrinsic(memcpy) +#endif + +#if 0 && defined(_MSC_VER) +#define rotl32 _lrotl +#define rotr32 _lrotr +#else +#define rotl32(x,n) (((x) << n) | ((x) >> (32 - n))) +#define rotr32(x,n) (((x) >> n) | ((x) << (32 - n))) +#endif + +#if !defined(bswap_32) +#define bswap_32(x) (rotr32((x), 24) & 0x00ff00ff | rotr32((x), 8) & 0xff00ff00) +#endif + +#if (PLATFORM_BYTE_ORDER == BRG_LITTLE_ENDIAN) +#define SWAP_BYTES +#else +#undef SWAP_BYTES +#endif + +#if defined(SWAP_BYTES) +#define bsw_32(p,n) \ + { int _i = (n); while(_i--) ((sha1_32t*)p)[_i] = bswap_32(((sha1_32t*)p)[_i]); } +#else +#define bsw_32(p,n) +#endif + +#define SHA1V1_MASK (SHA1V1_BLOCK_SIZE - 1) + +#if 0 + +#define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z))) +#define parity(x,y,z) ((x) ^ (y) ^ (z)) +#define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) + +#else /* Discovered by Rich Schroeppel and Colin Plumb */ + +#define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) +#define parity(x,y,z) ((x) ^ (y) ^ (z)) +#define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y)))) + +#endif + +/* Compile 64 bytes of hash data into SHA1 context. Note */ +/* that this routine assumes that the byte order in the */ +/* ctx->wbuf[] at this point is in such an order that low */ +/* address bytes in the ORIGINAL byte stream in this buffer */ +/* will go to the high end of 32-bit words on BOTH big and */ +/* little endian systems */ + +#ifdef ARRAY +#define q(v,n) v[n] +#else +#define q(v,n) v##n +#endif + +#define one_cycle(v,a,b,c,d,e,f,k,h) \ + q(v,e) += rotr32(q(v,a),27) + \ + f(q(v,b),q(v,c),q(v,d)) + k + h; \ + q(v,b) = rotr32(q(v,b), 2) + +#define five_cycle(v,f,k,i) \ + one_cycle(v, 0,1,2,3,4, f,k,hf(i )); \ + one_cycle(v, 4,0,1,2,3, f,k,hf(i+1)); \ + one_cycle(v, 3,4,0,1,2, f,k,hf(i+2)); \ + one_cycle(v, 2,3,4,0,1, f,k,hf(i+3)); \ + one_cycle(v, 1,2,3,4,0, f,k,hf(i+4)) + +void sha1v1_compile(sha1_ctx ctx[1]) +{ sha1_32t *w = ctx->wbuf; + +#ifdef ARRAY + sha1_32t v[5]; + memcpy(v, ctx->hash, 5 * sizeof(sha1_32t)); +#else + sha1_32t v0, v1, v2, v3, v4; + v0 = ctx->hash[0]; v1 = ctx->hash[1]; + v2 = ctx->hash[2]; v3 = ctx->hash[3]; + v4 = ctx->hash[4]; +#endif + +#define hf(i) w[i] + + five_cycle(v, ch, 0x5a827999, 0); + five_cycle(v, ch, 0x5a827999, 5); + five_cycle(v, ch, 0x5a827999, 10); + one_cycle(v,0,1,2,3,4, ch, 0x5a827999, hf(15)); \ + +#undef hf +#define hf(i) (w[(i) & 15] = rotl32( \ + w[((i) + 13) & 15] ^ w[((i) + 8) & 15] \ + ^ w[((i) + 2) & 15] ^ w[(i) & 15], 1)) + + one_cycle(v,4,0,1,2,3, ch, 0x5a827999, hf(16)); + one_cycle(v,3,4,0,1,2, ch, 0x5a827999, hf(17)); + one_cycle(v,2,3,4,0,1, ch, 0x5a827999, hf(18)); + one_cycle(v,1,2,3,4,0, ch, 0x5a827999, hf(19)); + + five_cycle(v, parity, 0x6ed9eba1, 20); + five_cycle(v, parity, 0x6ed9eba1, 25); + five_cycle(v, parity, 0x6ed9eba1, 30); + five_cycle(v, parity, 0x6ed9eba1, 35); + + five_cycle(v, maj, 0x8f1bbcdc, 40); + five_cycle(v, maj, 0x8f1bbcdc, 45); + five_cycle(v, maj, 0x8f1bbcdc, 50); + five_cycle(v, maj, 0x8f1bbcdc, 55); + + five_cycle(v, parity, 0xca62c1d6, 60); + five_cycle(v, parity, 0xca62c1d6, 65); + five_cycle(v, parity, 0xca62c1d6, 70); + five_cycle(v, parity, 0xca62c1d6, 75); + +#ifdef ARRAY + ctx->hash[0] += v[0]; ctx->hash[1] += v[1]; + ctx->hash[2] += v[2]; ctx->hash[3] += v[3]; + ctx->hash[4] += v[4]; +#else + ctx->hash[0] += v0; ctx->hash[1] += v1; + ctx->hash[2] += v2; ctx->hash[3] += v3; + ctx->hash[4] += v4; +#endif +} + +void sha1v1_begin(sha1_ctx ctx[1]) +{ + ctx->count[0] = ctx->count[1] = 0; + ctx->hash[0] = 0x67452301; + ctx->hash[1] = 0xefcdab89; + ctx->hash[2] = 0x98badcfe; + ctx->hash[3] = 0x10325476; + ctx->hash[4] = 0xc3d2e1f0; +} + +/* SHA1 hash data in an array of bytes into hash buffer and */ +/* call the hash_compile function as required. */ + +void sha1v1_hash( sha1_ctx ctx[1],const unsigned char data[], unsigned long len) +{ sha1_32t pos = (sha1_32t)(ctx->count[0] & SHA1V1_MASK), + space = SHA1V1_BLOCK_SIZE - pos; + const unsigned char *sp = data; + + if((ctx->count[0] += len) < len) + ++(ctx->count[1]); + + while(len >= space) /* tranfer whole blocks if possible */ + { + memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space); + sp += space; len -= space; space = SHA1V1_BLOCK_SIZE; pos = 0; + bsw_32(ctx->wbuf, SHA1V1_BLOCK_SIZE >> 2); + sha1v1_compile(ctx); + } + + memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len); +} + +/* SHA1 final padding and digest calculation */ + +void sha1v1_end(sha1_ctx ctx[1], unsigned char hval[]) +{ sha1_32t i = (sha1_32t)(ctx->count[0] & SHA1V1_MASK); + + /* put bytes in the buffer in an order in which references to */ + /* 32-bit words will put bytes with lower addresses into the */ + /* top of 32 bit words on BOTH big and little endian machines */ + bsw_32(ctx->wbuf, (i + 3) >> 2); + + /* we now need to mask valid bytes and add the padding which is */ + /* a single 1 bit and as many zero bits as necessary. Note that */ + /* we can always add the first padding byte here because the */ + /* buffer always has at least one empty slot */ + ctx->wbuf[i >> 2] &= 0xffffff80 << 8 * (~i & 3); + ctx->wbuf[i >> 2] |= 0x00000080 << 8 * (~i & 3); + + /* we need 9 or more empty positions, one for the padding byte */ + /* (above) and eight for the length count. If there is not */ + /* enough space, pad and empty the buffer */ + if(i > SHA1V1_BLOCK_SIZE - 9) + { + if(i < 60) ctx->wbuf[15] = 0; + sha1v1_compile(ctx); + i = 0; + } + else /* compute a word index for the empty buffer positions */ + i = (i >> 2) + 1; + + while(i < 14) /* and zero pad all but last two positions */ + ctx->wbuf[i++] = 0; + + /* the following 32-bit length fields are assembled in the */ + /* wrong byte order on little endian machines but this is */ + /* corrected later since they are only ever used as 32-bit */ + /* word values. */ + ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29); + ctx->wbuf[15] = ctx->count[0] << 3; + sha1v1_compile(ctx); + + /* extract the hash value as bytes in case the hash buffer is */ + /* misaligned for 32-bit words */ + for(i = 0; i < SHA1V1_DIGEST_SIZE; ++i) + hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3))); +} + +void sha1v1(unsigned char hval[], const unsigned char data[], unsigned long len) +{ sha1_ctx cx[1]; + + sha1v1_begin(cx); sha1v1_hash(cx, data, len); sha1v1_end(cx, hval); +} + +#if defined(__cplusplus) +} +#endif --- /dev/null 2003-04-09 14:48:48.000000000 +0200 +++ src/utils/lib/sha1v1.h 2004-02-04 18:18:09.000000000 +0100 @@ -0,0 +1,86 @@ +/* + --------------------------------------------------------------------------- + Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved. + + LICENSE TERMS + + The free distribution and use of this software in both source and binary + form is allowed (with or without changes) provided that: + + 1. distributions of this source code include the above copyright + notice, this list of conditions and the following disclaimer; + + 2. distributions in binary form include the above copyright + notice, this list of conditions and the following disclaimer + in the documentation and/or other associated materials; + + 3. the copyright holder's name is not used to endorse products + built using this software without specific written permission. + + ALTERNATIVELY, provided that this notice is retained in full, this product + may be distributed under the terms of the GNU General Public License (GPL), + in which case the provisions of the GPL apply INSTEAD OF those given above. + + DISCLAIMER + + This software is provided 'as is' with no explicit or implied warranties + in respect of its properties, including, but not limited to, correctness + and/or fitness for purpose. + --------------------------------------------------------------------------- + Issue Date: 26/08/2003 +*/ + +#ifndef _SHA1V1_H +#define _SHA1V1_H + +#include + +#define SHA1V1_BLOCK_SIZE 64 +#define SHA1V1_DIGEST_SIZE 20 + +#if defined(__cplusplus) +extern "C" +{ +#endif + +/* define an unsigned 32-bit type */ + +#if defined(_MSC_VER) + typedef unsigned long sha1_32t; +#elif defined(ULONG_MAX) && ULONG_MAX == 0xfffffffful + typedef unsigned long sha1_32t; +#elif defined(UINT_MAX) && UINT_MAX == 0xffffffff + typedef unsigned int sha1_32t; +#else +# error Please define sha1_32t as an unsigned 32 bit type in sha1v1.h +#endif + +/* type to hold the SHA256 context */ + +typedef struct +{ sha1_32t count[2]; + sha1_32t hash[5]; + sha1_32t wbuf[16]; +} sha1_ctx; + +/* Note that these prototypes are the same for both bit and */ +/* byte oriented implementations. However the length fields */ +/* are in bytes or bits as appropriate for the version used */ +/* and bit sequences are input as arrays of bytes in which */ +/* bit sequences run from the most to the least significant */ +/* end of each byte */ + +#define SHA1V1_CTX sha1_ctx + +void sha1v1_compile(sha1_ctx ctx[1]); + +void sha1v1_begin(sha1_ctx ctx[1]); +void sha1v1_hash( sha1_ctx ctx[1],const unsigned char data[], unsigned long len); +void sha1v1_end(sha1_ctx ctx[1],unsigned char hval[]); +void sha1v1(unsigned char hval[], const unsigned char data[], unsigned long len); + +#if defined(__cplusplus) +} +#endif + +#endif --- /dev/null 2003-04-09 14:48:48.000000000 +0200 +++ tools/sha1bench.ml 2004-02-04 19:36:15.000000000 +0100 @@ -0,0 +1,48 @@ +open Md4 +open LittleEndian +open Unix +open Printf2 +open Obj + +let num = ref 1 + +let benchsha1 filename= + let results = [|0.;0.|] in + let s = Unix32.create_rw filename in + let length = Unix32.getsize64 s in + Printf.printf "test : %s\n-----------------------------------\n" + filename; + Printf.printf "Testing RFC: "; + let sha1 = Sha1.digest_subfile s + Int64.zero length in + for i = 1 to !num do + let beforehash = gettimeofday () in + let sha1 = Sha1.digest_subfile s + Int64.zero length in + results.(0)<- results.(0) +. + (gettimeofday () -. beforehash); + Printf.printf "."; + done; + Printf.printf "\nTesting Brian Gladman's implementation : "; + for i = 1 to !num do + let beforehash = gettimeofday () in + let sha1v1 = Sha1v1.digest_subfile s + Int64.zero length in + results.(1)<- results.(1) +. + (gettimeofday () -. beforehash); + Printf.printf "%s" (if (Sha1v1.to_string sha1v1)= + (Sha1.to_string sha1) then "." else "X") + done; + Printf.printf "\nResults : \n RFC : average %f \n BrG : average %f \n" + (results.(0)/.(float_of_int !num)) (results.(1)/.(float_of_int !num)) + ; + Printf.printf "(RFC-Brg)/RFC : %f\n\n" ((results.(0)-.results.(1))/.results.(0)) + +let _ = + Arg.parse [ + "-r", Arg.Int ((:=) num), " : Set the number of iteration of sha1 benchmark (default = 1)" + ] (fun filename -> + benchsha1 filename + ) " : benshmark sha1 implementations against filenames" + +