I wrote this a couple of years ago. It's a bit more bit fiddly, and I know
it works as it was used in production to talk to an exchange.

NB. hmac signature
NB. x key, y needs signature
NB. key may need to be made raw first
hmac=: 4 : 0
 key=. x,(128 - #x) # 0{a.
 ixorkey =.  ((22 b.)&(16b36) a. i. key){a.
 raw=. _5 (128!:6) ixorkey,y
 oxorkey =. ((22 b.)&(16b5c) a. i. key){a.
 5 (128!:6) oxorkey,raw
)

testhmac=: 3 : 0

assert.
'97b46378f86d10f5be4ed64051fb84150bdfcf0997f78b36f7f0db64f2725d29b52f354d14fb30dcef231dac73579d0961788b0e7540dd46b5d3e70c625135d7'
= '' hmac val

assert.
'62bcafb5c93d462f98ef56aa64015287a7f33eb9cf9914dfa2beb77132429c2cfe0a84ab6685cf757d911e13c12de30b15bbdda4ec280969195c7a0b52cc2ece'
= '98' hmac val

assert.
'4cc79cd8cfb58f240579de601e17cfba4d050799d5e54f100df596ac8f643121441ebc74a3a0c1010a3a95a8318b50d7d0d04598dc3d351598dd32b615d6f243'=
'980000' hmac val

assert.'9e96c80f25627abfce560ddc097a25526a33e19a9b9722674c9026e481c6e77a5ee4fe6ecb9d2e35280964a005790463332c6d35ac2b9568ca311719b81a3bf7'
= 'cdca1c8b2fed44acad6d87299b344d88' hmac val

)


key =. '22728a5858822d984f247b273c1fb44d'

val=. '
https://bittrex.com/api/v1.1/market/selllimit?apikey=&market=BTC-BAT&quantity=5000&rate=1.903e-05&nonce=1512336993
'

val2 =. 'junkjunkjunk'

testhmacpublic =: 3 : 0

assert.
'97b46378f86d10f5be4ed64051fb84150bdfcf0997f78b36f7f0db64f2725d29b52f354d14fb30dcef231dac73579d0961788b0e7540dd46b5d3e70c625135d7'
= '' hmac val

assert.
'62bcafb5c93d462f98ef56aa64015287a7f33eb9cf9914dfa2beb77132429c2cfe0a84ab6685cf757d911e13c12de30b15bbdda4ec280969195c7a0b52cc2ece'
= '98' hmac val

assert.
'4cc79cd8cfb58f240579de601e17cfba4d050799d5e54f100df596ac8f643121441ebc74a3a0c1010a3a95a8318b50d7d0d04598dc3d351598dd32b615d6f243'=
'980000' hmac val

assert.
'e1161d4fad6bd76481726f63de7836ed1b36d4d8b5dd6ba6b203376c0f7e355af3831207919995384d2e0498858f4ef494bfbc119ed6cc5cd9924da572343c55'
= key hmac val

assert.'4e33fdd9f961f824a2868ed34cb793a6e3d645ee3a52aaf2ec2f30d2ec4ac18dc615bcbe98ef608dc28bf87f75fca13e5c8397248a88dc3425d675e9263e605f'
= '' hmac val2

assert.
'987a994611674efa06944b64dbc47d09a7bd1c133eab07c72b9938ac9367d4c3cafda2fe3b5e1eaa2c6a100a0d12c738b3ff2942131f98eb94f9d0224390970a'
= '09' hmac val2

assert
'ec072266fd115c5bd3f91d8d07133a2ee70a9930be4efa31daf7f0df082169ce9dc5f99d401a4e6a7c237248fe2deef521465f818a09a5ad7788027589a2227c'
= key hmac val2

)


testhmacpublic ''


testhmac''



On Wed, Feb 2, 2022 at 2:52 PM Raul Miller <[email protected]> wrote:

> On Wed, Feb 2, 2022 at 4:41 AM 'Viktor Grigorov' via Programming
> <[email protected]> wrote:
> > I wanted to see if J could be faster than bespoke C programs
> (specifically aircrack-ng, ~10k/s on my machine).
> > From the wiki pages for SHA1 (turns outs there exists a foreign so I
> dropped it), HMAC, and PBKDF2I assembled:
> > sha1=:1&(128!:6)
> > bs=:512%8 NB. block size in bytes for md5, sha1, sha224, sha256
> > opad=:bs(([,(#@]))$])0 1 0 1 1 1 0 0 NB. 0x5c, outer padding byte up to
> block size
> > ipad=:bs(([,(#@]))$])0 0 1 1 0 1 1 0 NB. 0x36, inner padding byte up to
> block size
> >
> > hmac=:{{)a NB. assuming y of form 'key';'msg'
> > if.bs&<@#key=.{.>y do.key=.u key end. NB. use hash of key as key, if
> greater than block size
> > key=.((],(((bs&-@#),8:)$0:))@((8:#2:)#:(a.&i.)))key NB. pad 0s to right
> up to block size; do 8 bits of ascii
> > u(({&a.@#.(key~:opad)),u(({&a.@#.(key~:ipad)),{:>y))
> > }}
> >
> > sha1 hmac 'key';';msg'
> > I am getting the wrong hashes back with examples from the wiki examples,
> ones generated from https://cryptotools.net/hmac What exactly am I
> missing?
>
> One issue (not the only issue) I think has to do with how you pad the key.
>
> If I understand https://en.wikipedia.org/wiki/HMAC correctly, this
> will pad the key:
>
> padkey=: {{
>   if.x < # y do. u y
>   else. x{.y,x#{.a.
>   end.
> }}
>
> And,  I should expect that sha1 hmac (bs sha1 padkey 'key');';msg'
> would have the same result as sha1 hmac 'key';';msg'
>
> I haven't looked any deeper than this, here.
>
> Good luck,
>
> --
> Raul
> ----------------------------------------------------------------------
> 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