@@ -17,17 +17,30 @@ <h3>BoundAuth Generator</h3>
1717 < p > < b > Result:</ b > < code id ="output "> –</ code > </ p >
1818
1919 < script >
20- async function computeHMAC ( password , ip , sharedSecret = "" ) {
21- const keyData = new TextEncoder ( ) . encode ( password + sharedSecret ) ;
20+ async function hmacSHA256 ( keyBytes , messageBytes ) {
2221 const key = await crypto . subtle . importKey (
23- "raw" , keyData ,
22+ "raw" , keyBytes ,
2423 { name : "HMAC" , hash : "SHA-256" } ,
2524 false , [ "sign" ]
2625 ) ;
26+ return new Uint8Array ( await crypto . subtle . sign ( "HMAC" , key , messageBytes ) ) ;
27+ }
2728
29+ async function computeHMAC ( password , ip , sharedSecret = "" ) {
2830 const ipBin = parseIP ( ip ) ;
29- const signature = await crypto . subtle . sign ( "HMAC" , key , ipBin ) ;
30- return base32encode ( new Uint8Array ( signature ) ) . slice ( 0 , parseInt ( document . getElementById ( "length" ) . value , 10 ) ) ;
31+ const pwBytes = new TextEncoder ( ) . encode ( password ) ;
32+
33+ const inner = await hmacSHA256 ( pwBytes , ipBin ) ; // inner = HMAC(user_secret, ip)
34+
35+ let final ;
36+ if ( sharedSecret . length > 0 ) {
37+ const sharedBytes = new TextEncoder ( ) . encode ( sharedSecret ) ;
38+ final = await hmacSHA256 ( sharedBytes , inner ) ; // final = HMAC(server_secret, inner)
39+ } else {
40+ final = inner ;
41+ }
42+
43+ return base32encode ( final ) . slice ( 0 , parseInt ( document . getElementById ( "length" ) . value , 10 ) ) ;
3144 }
3245
3346 function parseIP ( ip ) {
0 commit comments