SHA-256: Difference between revisions
Content added Content deleted
(Add Seed7 example) |
No edit summary |
||
Line 3: | Line 3: | ||
Either by using a dedicated library or implementing the algorithm in your language, show that the SHA-256 digest of the string "Rosetta code" is: 764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf |
Either by using a dedicated library or implementing the algorithm in your language, show that the SHA-256 digest of the string "Rosetta code" is: 764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf |
||
=={{header|AutoHotkey}}== |
|||
<lang AutoHotkey>rosetta := "Rosetta code" |
|||
MsgBox % "String:`t" rosetta "`nSHA-256:`t" SHA256(rosetta) |
|||
; MD4 =============================================================================== |
|||
SHA256(string, encoding = "utf-8") |
|||
{ |
|||
return CalcStringHash(string, 0x800c, encoding) |
|||
} |
|||
; CalcAddrHash ====================================================================== |
|||
CalcAddrHash(addr, length, algid, byref hash = 0, byref hashlength = 0) |
|||
{ |
|||
static h := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"] |
|||
static b := h.minIndex() |
|||
o := "" |
|||
if (DllCall("advapi32\CryptAcquireContext", "Ptr*", hProv, "Ptr", 0, "Ptr", 0, "Uint", 24, "Uint", 0xF0000000)) |
|||
{ |
|||
if (DllCall("advapi32\CryptCreateHash", "Ptr", hProv, "Uint", algid, "Uint", 0, "Uint", 0, "Ptr*", hHash)) |
|||
{ |
|||
if (DllCall("advapi32\CryptHashData", "Ptr", hHash, "Ptr", addr, "Uint", length, "Uint", 0)) |
|||
{ |
|||
if (DllCall("advapi32\CryptGetHashParam", "Ptr", hHash, "Uint", 2, "Ptr", 0, "Uint*", hashlength, "uint", 0)) |
|||
{ |
|||
VarSetCapacity(hash, hashlength, 0) |
|||
if (DllCall("advapi32\CryptGetHashParam", "Ptr", hHash, "Uint", 2, "Ptr", &hash, "Uint*", hashlength, "Uint", 0)) |
|||
{ |
|||
Loop, % hashlength |
|||
{ |
|||
v := NumGet(hash, A_Index - 1, "uchar") |
|||
o .= h[(v >> 4) + b] h[(v & 0xf) + b] |
|||
} |
|||
} |
|||
} |
|||
} |
|||
DllCall("advapi32\CryptDestroyHash", "Ptr", hHash) |
|||
} |
|||
DllCall("advapi32\CryptReleaseContext", "Ptr", hProv, "Uint", 0) |
|||
} |
|||
return o |
|||
} |
|||
; CalcStringHash ==================================================================== |
|||
CalcStringHash(string, algid, encoding = "utf-8", byref hash = 0, byref hashlength = 0) |
|||
{ |
|||
chrlength := (encoding = "cp1200" || encoding = "utf-16") ? 2 : 1 |
|||
length := (StrPut(string, encoding) - 1) * chrlength |
|||
VarSetCapacity(data, length, 0) |
|||
StrPut(string, &data, floor(length / chrlength), encoding) |
|||
return CalcAddrHash(&data, length, algid, hash, hashlength) |
|||
}</lang> |
|||
{{out}} |
|||
<pre>String: Rosetta Code |
|||
SHA-256: 764FAF5C61AC315F1497F9DFA542713965B785E5CC2F707D6468D7D1124CDFCF</pre> |
|||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |