User:Pascal Jasmin/SHA 1(2c20)2 and MD5 for windows

From J Wiki
Jump to navigation Jump to search

It would be nice to know if this is usable with Wine. The dll used comes with windows: advapi32.dll. Despite the name, it may be 64 bit afaik. The dll is listed as wine compatible.

Code file is at bottom,

usage:

   sha256 hexhash 'The quick brown fox jumps over the lazy dog'
d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592

sha256 is specialized version of Hash function.  hexhash is an adverb that specifies format of output.

   CALG_SHA_256 Hash hexhash L:0  'The quick brown fox jumps over the lazy dog';''
┌────────────────────────────────────────────────────────────────┬────────────────────────────────────────────────────────────────┐
│d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592│e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855│
└────────────────────────────────────────────────────────────────┴────────────────────────────────────────────────────────────────┘
   CALG_MD5 Hash listhash 'The quick brown fox jumps over the lazy dog'
158 16 125 157 55 43 182 130 107 216 29 53 66 164 25 214

   CALG_SHA1 Hash bighash  'The quick brown fox jumps over the lazy dog'
273069992013452546326057769888623105462687230738

   CALG_SHA_512 Hash b64hash  'The quick brown fox jumps over the lazy dog'
B+VH2VhvanP3P7rAQ17XaVEhj7fQyNeIownXhUNru2Quk6JSqVTyORJUfR6KO17W4b/XCXghIz+gU489uFT+5g==

   sha256 int32hash 'The quick brown fox jumps over the lazy dog'
3618175923 131563668 1774885564 2953326159 2371244516 1832704886 755159231 935978386

   ts
,&'/sec '@":@%@(6!:2) , ,&'MB'@":@(%&1000000)@(7!:2)@:]


   ts 'CALG_SHA_256 Hash L:0 ]   100$<''The quick brown fox jumps over the lazy dog'''
57.8489/sec 0.025472MB

NB. timings can vary from run to run, but these are about right
   100 ts 'sha256 listhash ''The quick brown fox jumps over the lazy dog'''
5783.29/sec 0.010112MB
   100 ts 'sha256 bighash ''The quick brown fox jumps over the lazy dog'''
5036.67/sec 0.036736MB
   100 ts 'sha256 hexhash ''The quick brown fox jumps over the lazy dog'''
5264.49/sec 0.020352MB

Performance of all the algorithms seem about the same. Close to 6000 hashes per second.

require 'dll'

ALG_SID_SHA_256 =: 12
ALG_CLASS_HASH =: 32768
PROV_RSA_FULL =:1
PROV_RSA_AES =: 24
ALG_CLASS_HASH =: 32768
ALG_TYPE_ANY =: 0
CALG_SHA_256  =: 32 32780 NB.ALG_CLASS_HASH +. ALG_TYPE_ANY +. ALG_SID_SHA_256
CALG_SHA1 =: 20 16b8004
CALG_SHA_384=: 48 16b800d
CALG_SHA_512=: 64 16b800e
CALG_MD5 =: 16 16b8003

CALG_AES_128=: 16 16b660e
CALG_AES_192=: 24 16b660f
CALG_AES_256=: 32 16b6610

HP_HASHVAL =: 2
CRYPT_VERIFYCONTEXT =: 4026531840
CryptAcquireContext =: ('advapi32.dll CryptAcquireContextA i *x *c *c i i') & cd
NB.  (ByRef phProv As Long, ByVal pszContainer As String, ByVal pszProvider As String, ByVal dwProvType As Long, ByVal dwFlags As Long) As Long
CryptReleaseContext =: 'advapi32.dll CryptReleaseContext i i i' & cd
NB. (ByVal hProv As Long, ByVal dwFlags As Long) As Long
CryptCreateHash =: 'advapi32.dll CryptCreateHash i x i i i *x' & cd
NB.(ByVal hProv As Long, ByVal Algid As Long, ByVal hKey As Long, ByVal dwFlags As Long, ByRef phHash As Long) As Long
CryptDestroyHash =: 'advapi32.dll CryptDestroyHash i i' & cd
NB.(ByVal hHash As Long) As Long
CryptHashData =: 'advapi32.dll CryptHashData i x *c i i' & cd
NB.(ByVal hHash As Long, pbData As Any, ByVal cbData As Long, ByVal dwFlags As Long) As Long
CryptGetHashParam =: 'advapi32.dll CryptGetHashParam i x i *c *i i' & cd



Hash =: 4 : 0
CryptAcquireContext(aProv =. , _1);(<0);(<0);PROV_RSA_AES;CRYPT_VERIFYCONTEXT
'size alg' =. x
CryptCreateHash ({. aProv) ; (alg) ; 0 ; 0 ; (m=. , _1)
CryptHashData ({. m) ; (y) ; (# y) ; 0
CryptGetHashParam ({. m); HP_HASHVAL ; ((o2=. , size # ' ' ) ); (,size) ; 0
CryptDestroyHash ({. m)
out =. o2
 CryptReleaseContext ({. aProv);0
 out
)

sha256 =: 3 : 0  NB. NOTIMPL: optional x is 'pads between rounds'; num rounds
CryptAcquireContext(aProv =. , _1);(<0);(<0);PROV_RSA_AES;CRYPT_VERIFYCONTEXT
'size alg' =. CALG_SHA_256
NB.'pad num' =. x
CryptCreateHash ({. aProv) ; (alg) ; 0 ; 0 ; (m=. , _1)
CryptHashData  ({. m) ; (y) ; (# y) ; 0 NB. chd y
CryptGetHashParam ({. m); HP_HASHVAL ; ((o2=. , size # ' ' ) ); (,size) ; 0
CryptDestroyHash ({. m)
CryptReleaseContext ({. aProv);0
o2
)
int32hash =: (_4 tobyte32 inv\ a.i.])@:
b64hash =: tobase64@:
hexhash =: ( [: ,@:hfd a.i.])@:
bighash =: (256x #. a. i. ])@
tobyte64 =:  (8 # 256) #: ]
tobyte32 =: (4 # 256) #: ]

Cross Platform -openssl version

An openssl version substantially faster and cross platform: with sha1, RIPEMD160, sha256 and sha512 (all are same speed on win64)

with help from Bill Lam, these should work on 32 and 64 bit systems. Note first 2 lines for location of libeay32.dll

require 'dll'
sslp =: IFWIN pick '/usr/lib/';'D:\OpenSSL-Win64\bin\'
sslp =: IFWIN pick ''; '/',~ jpath '~bin'  NB. with J802.  cut this line if you wish to point to downloaded folder
OPENSSL =: sslp , (IFIOS + (;: 'Win Linux Android Darwin') i. <UNAME_z_) pick 'libeay32.dll '; (2 $ <'libssl.so.1.0.0 '),  (2 $ <'/usr/lib/libssl.dylib ')
SSLE =: sslp , '\openssl'
ssl =: 1 : '(OPENSSL , m)&cd'
sslRMD160 =: (IFWIN {:: ' RIPEMD160 > + x *c x *c';'RIPEMD160 > x *c x *c')  ssl
sslSha256 =: (IFWIN {:: ' SHA256 > + x *c x *c';'SHA256 > x *c x *c')  ssl  NB. SHA256 i *c l *c' ssl
sslSha512 =: (IFWIN {:: ' SHA512 > + x *c x *c';'SHA512 > x *c x *c')  ssl
sslSha1 =: (IFWIN {:: ' SHA1 > + x *c x *c';'SHA1 > x *c x *c')  ssl
sslMD5 =: (IFWIN {:: ' MD5 > + x *c x *c';'MD5 > x *c x *c')  ssl

sr160=: 3 : 0
sslRMD160 (y);(#y);md=. 20#' '
md
)

sha1=: 3 : 0
sslSha1 (y);(#y);md=. 20#' '
md
)
s512=: 3 : 0
sslSha512 (y);(#y);md=. 64#' '
md
)
s256=: 3 : 0
sslSha256 (y);(#y);md=. 32#' '
md
)
md5 =: 3 : 0
sslMD5 (y);(# , y);md=. 16#' '
md
)

   s256 hexhash 'The quick brown fox jumps over the lazy dog'
d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592

   s256 hexhash 'The quick brown fox jumps over the lazy dog.'
ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c

   100 ts 's256  ''The quick brown fox jumps over the lazy dog'''
105932/sec 0.005888MB