Below, is a generalization conjunction hmac for what is available in
128!:6, tests are for sha1, to check correctness wtr of alg/padding,
m - is a key string
n - the same meaning as on 128!:6


bfh=:a.{~_2(16&#.)\'0123456789abcdef'i.] NB. bytes from hex string

'key' hmac 1 'asdf'

384d71757bad0ee72927860b654958dc9b3be9cf

'key' hmac _1 'asdf'

8Mqu{� �)'� eIXܛ;��

('key' hmac _1 'asdf') -: bfh 'key' hmac 1 'asdf'

1



===

NB. from https://en.wikipedia.org/wiki/Secure_Hash_Algorithms

0 : 0 NB. id at 128!: (MD5 for reference only), name, bit out len, bit
block size

0 MD5 128 512

1 SHA1 160 512

2 SHA224 224 512

3 SHA256 256 512

4 SHA384 384 1024

5 SHA512 512 1024

6 SHA3_224 224 1152

7 SHA3_256 256 1088

8 SHA3_384 384 832

9 SHA3_512 512 576

)


hmac =: 2 : 0 NB. m is a key data, n is as 128!:6, result - a monad to sign
y

H =. (-|n)&(128!:6) NB. select hash function

BS =. (|n) { 64 64 64 64 128 128 144 136 104 72 NB. select pad len / block
size of a hash function

if. BS<#m do. K =. H m else. K=. m end. NB. if m is longer than pad (block
size of hash) compress key by hashing

K =. BS {.a.i.K NB. extend to BS the key pad data with zero bytes

'i o'=.a.{~16b36 16b5c (22 b.)"0 1 K NB. compute input and output pads from
the key bytes

n 128!:6 o, H i,y NB. calculate the final signature

)


NB. tests https://tools.ietf.org/html/rfc2202 HMAC-SHA1

testHmacSha1 =. 3 : 0

assert. 'b617318655057264e28bc0b6fb378c8ef146be00' -: (20#16b0b{a.) hmac 1
'Hi There'

assert. 'effcdf6ae5eb2fa2d27416d5f184df9c259a7c79' -: 'Jefe' hmac 1 'what
do ya want for nothing?'

assert. '125d7342b9ac11cd91a39af48aa17b4f63f175d3' -: (20#16baa{a.) hmac 1
[50 # 16bdd{a.

assert. '4c1a03424b55e07fe7f27be1d58bb9324a9a5a04' -: (20#16b0c{a.) hmac 1
'Test With Truncation'

assert. 'aa4ae5e15272d00e95705637ce8a3b55ed402112' -: (80#16baa{a.) hmac 1
'Test Using Larger Than Block-Size Key - Hash Key First'

assert. 'e8e99d0f45237d786d6bbaa7965c7808bbff1a91' -: (80#16baa{a.) hmac 1
'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data'

assert. 'aa4ae5e15272d00e95705637ce8a3b55ed402112' -: (80#16baa{a.) hmac 1
'Test Using Larger Than Block-Size Key - Hash Key First'

assert. 'e8e99d0f45237d786d6bbaa7965c7808bbff1a91' -: (80#16baa{a.) hmac 1
'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data'

)

testHmacSha1''


2017-12-22 13:32 GMT+03:00 Danil Osipchuk <[email protected]>:

> I know, but I had to use md5 - I remember wondering why it is not there.
> The same issue had the person writing jqt where I borrowed  md5
>
>
>
> 2017-12-22 13:16 GMT+03:00 bill lam <[email protected]>:
>
>> j806 j engine supports various sha, both hex and byte output.
>>
>> On Dec 22, 2017 4:51 PM, "Danil Osipchuk" <[email protected]>
>> wrote:
>>
>> > Below is a snippet I used to sign files with md5hmac, may be of some
>> use.
>> > (I envy people easily reading other's J, I have difficulties reading
>> mine a
>> > while later )
>> > regards,
>> >   Danil
>> >
>> >
>> > qthash =: 4 : 0                        NB. taken from qt ide, x is hash
>> > algo name str, y is a data string, result is hex string of hash
>> >   c=.'"',libjqt,'" gethash ',(IFWIN#'+'),' i *c *c i * *i'
>> >   'r t m w p n'=. c cd (tolower x);y;(#y);(,2);,0
>> >   res=. memr p,0,n
>> >   if. r do. res (13!:8) 3 end.
>> >   res
>> > )
>> > bfh=:a.{~_2(16&#.)\'0123456789abcdef'i.]    NB. bytes from hex string
>> > (lowercase) (253*254)-:bfh 'fefc'
>> > md5b=: [:bfh 'md5'&qthash            NB. libjqt returns hex string as a
>> > hash - convert it to bytes
>> > hmac =: 1 : 0                         NB. m is a key data, return verb
>> > computing md5-based signature for y
>> >  if. 64<#m do. k=.md5b m else.k=.m end.        NB. if m is longer than
>> pad
>> > (64 - block of md5) use md5 hash as a key
>> >  kb=.64{.a.i.k                        NB. extend to 64 the key pad data
>> > with zero bytes
>> >  'i o'=.a.{~16b36 16b5c (22 b.)"0 1 kb        NB. compute input and
>> output
>> > pads from the key bytes
>> >  md5b o, md5b i,y                NB. calculate the final signature as in
>> > wikipedia
>> > )
>> > md5SgnDir =: 3 : 0                 NB. sign like CryproSign.jar:
>> md5SgnDir
>> > (<;._1)' ../sandbox *.cdr .md5 ../z/sssti_m7ium1_2016-05-25/
>> > crypto/key.txt'
>> >  'dr ms ex kf'=. y                NB. directory name, mask of the
>> filename,
>> > extension added to md5 sign file, key file name
>> >   mk=.(fread kf)hmac                    NB. load a keyfile into a verb
>> for
>> > computing signature
>> >  ((mk@:fread"0)fwrite"1 0(,&ex&.>"0))dr fdls ms    NB. calculate the
>> > signature of the content of each file and write to extended name
>> > )
>> >
>> > 2017-12-22 4:32 GMT+03:00 bill lam <[email protected]>:
>> >
>> > > if possible, please provide some examples or tests and I will add
>> them to
>> > > addons. Thanks
>> > > ------------------------------------------------------------
>> ----------
>> > > For information about J forums see http://www.jsoftware.com/forum
>> s.htm
>> > >
>> > ----------------------------------------------------------------------
>> > For information about J forums see http://www.jsoftware.com/forums.htm
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
>>
>
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to