Actually, for the general case, the hash function may have an output
size smaller than the block size.
So:
padkey=: {{
if.x < # y do. y=. u y end.
x{.y,x#{.a.
}}
FYI,
--
Raul
On Wed, Feb 2, 2022 at 6:51 AM 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