Time-based one-time password algorithm: Difference between revisions

m
→‎{{header|Wren}}: Minor tidy and rerun
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="ecmascriptwren">import "os" for Process
import "./long" for ULong
import "./crypto" for Bytes, Sha1, Sha256, Hmac
Line 986 ⟶ 1,019:
<pre>
$ date '+%s'
1707913906
1638830494
$ wren-cli totpTime-based_one-time_password_algorithm.wren 16388304941707913906
260040
38559208
24454221
185760458
189816119
556237229
499920719
</pre>
 
9,479

edits