I'm using: https://jwt.io/.
This is my code:
Code: Select all
Header =
(
{
"alg": "HS256",
"typ": "JWT"
}
)
Payload =
(
{
"sub": "1234567890",
"name": "JohnDoe",
"iat": 1516239022
}
)
header:= b64Encode(RegExReplace(Header, "(\n)|(\s)"))
; eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
payload:=b64Encode(RegExReplace(Payload, "(\n)|(\s)"))
; eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG5Eb2UiLCJpYXQiOjE1MTYyMzkwMjJ9
; Those two are okay.
tokenHash := bcrypt_sha256_hmac(RegExReplace(header, "(\n)|(\s)") . "." . RegExReplace(payload, "(\n)|(\s)"), "test123")
; 3b59324118bcd59a5435194120c2cfcb7cf295f25a79149b79145696329ffb95
msgbox % b64Encode(tokenHash)
; Different from O1kyQRi81ZpUNRlBIMLPy3zylfJaeRSbeRRWljKf-5U
b64Encode(string)
{
VarSetCapacity(bin, StrPut(string, "UTF-8")) && len := StrPut(string, &bin, "UTF-8") - 1
if !(DllCall("crypt32\CryptBinaryToString", "ptr", &bin, "uint", len, "uint", 0x40000001, "ptr", 0, "uint*", size))
throw Exception("CryptBinaryToString failed", -1)
VarSetCapacity(buf, size << 1, 0)
if !(DllCall("crypt32\CryptBinaryToString", "ptr", &bin, "uint", len, "uint", 0x40000001, "ptr", &buf, "uint*", size))
throw Exception("CryptBinaryToString failed", -1)
return StrGet(&buf)
}
So I tried using this code:"... which is a HMAC-SHA256 value, but not Base64url encoded. This hash is a hexadecimal string representation of a large number. To compare it with the value from https://jwt.io you need to convert the value from it's hexadecimal string representation back to a number and Base64url encode it."
Code: Select all
tokenHash := "3b59324118bcd59a5435194120c2cfcb7cf295f25a79149b79145696329ffb95"
token := HexToString(tokenHash)
msgbox % b64Encode(token)
HexToString(String)
{
local Length, CharStr, RetString
If !String
Return 0
Length := StrLen(String)//2
Loop, %Length%
{
StringMid, CharStr, String, A_Index*2 - 1, 2
CharStr = 0x%CharStr%
;Build the return string
RetString .= Chr(CharStr)
}
;Return the string to the caller
Return RetString
}
Thanks in advance.