Time-based one-time password algorithm: Difference between revisions
Time-based one-time password algorithm (view source)
Revision as of 12:34, 14 February 2024
, 3 months ago→{{header|Wren}}: Minor tidy and rerun
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
m (→{{header|Wren}}: Minor tidy and rerun) |
||
(One intermediate revision by one other user not shown) | |||
Line 315:
fmt.Println(code)
}</syntaxhighlight>
=={{header|J}}==
<syntaxhighlight lang="j">DA =: '0123456789ABCDEF'
to_bytes =: 16 16#.(2,~2%~#)$ DA i. ]
upper =: 1&(3!:12)
xor =: 22 b.
and =: 17 b.
hmac_sha1 =: {{
sha1 =. _1&(128!:6)
b_size =. 512 % 8
pad_key =. b_size {.]
block_sized_key =. pad_key a.&i. x
o_key_pad =. block_sized_key xor b_size $ 16b5c
i_key_pad =. block_sized_key xor b_size $ 16b36
hashed =. sha1 (i_key_pad { a.), y
a. i. sha1 (o_key_pad { a.), hashed }}
totp =: {{
h =. x hmac_sha1&:(a. {~ to_bytes&]) y
offset =. 16bf and {: h
1000000|16b7fffffff and (4$256)#. 4 {. offset |. h }}
</syntaxhighlight>
{{out|Example use}}
<syntaxhighlight lang="j">time =: '0000000000000001' NB. 64-bit timestamp in hex format
secrete =: 'AB54A98CEB1F0AD2' NB. secrete key in hex format
time totp secrete NB. 758742
</syntaxhighlight>
=={{header|Julia}}==
Line 904 ⟶ 937:
{{libheader|Wren-srandom}}
As Wren-cli currently has no way of determining the Unix time, this needs to be input as a command line parameter so we can track it from there.
<syntaxhighlight lang="
import "./long" for ULong
import "./crypto" for Bytes, Sha1, Sha256, Hmac
Line 986 ⟶ 1,019:
<pre>
$ date '+%s'
1707913906
$ wren-cli
260040
38559208
185760458
556237229
</pre>
|