Hash from string, hex or file, HMAC, Salt

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Hash from string, hex or file, HMAC, Salt

16 May 2014, 01:47

[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Hash from string, hex or file, HMAC, Salt

24 May 2016, 05:29

CRC32 (Cyclic redundancy check)
Implementation

Code: Select all

CRC32(str)
{
    static table := []
    loop 256 {
        crc := A_Index - 1
        loop 8
            crc := (crc & 1) ? (crc >> 1) ^ 0xEDB88320 : (crc >> 1)
        table[A_Index - 1] := crc
    }
    crc := ~0
    loop, parse, str
        crc := table[(crc & 0xFF) ^ Asc(A_LoopField)] ^ (crc >> 8)
    return Format("0x{:x}", ~crc)
}
example:

Code: Select all

MsgBox % CRC32("The quick brown fox jumps over the lazy dog")
output:

Code: Select all

0x414fa339

DllCall / WinAPI

Code: Select all

CRC32(str, enc = "UTF-8")
{
    l := (enc = "CP1200" || enc = "UTF-16") ? 2 : 1, s := (StrPut(str, enc) - 1) * l
    VarSetCapacity(b, s, 0) && StrPut(str, &b, floor(s / l), enc)
    CRC32 := DllCall("ntdll.dll\RtlComputeCrc32", "UInt", 0, "Ptr", &b, "UInt", s)
    return Format("0x{:x}", CRC32)
}
example:

Code: Select all

MsgBox % CRC32("The quick brown fox jumps over the lazy dog")
output:

Code: Select all

0x414fa339
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Hash from string, hex or file, HMAC, Salt

24 May 2016, 09:26

Custom hashing algorithm's

RSHash (Robert Sedgewick's string hashing algorithm)

Implementation

Code: Select all

RSHash(str)
{
    a := 0xF8C9, b := 0x5C6B7, h := 0
    loop, parse, str
        h := h * a + Asc(A_LoopField), a *= b
    return (h & 0x7FFFFFFF)
}
example & output

Code: Select all

MsgBox % RSHash("jdfgsdhfsdfsd 6445dsfsd7fg/*/+bfjsdgf%$^") ; <== 2012450421

JSHash (A bitwise hash function written by Justin Sobel)

Implementation

Code: Select all

JSHash(str)
{
    h := 0x4E67C6A7
    loop, parse, str
        h ^= ((h << 5) + Asc(A_LoopField) + (h >> 2))
    return (h & 0x7FFFFFFF)
}
example & output

Code: Select all

MsgBox % JSHash("jdfgsdhfsdfsd 6445dsfsd7fg/*/+bfjsdgf%$^") ; <== 1103173151
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
JoeWinograd
Posts: 2206
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Hash from string, hex or file, HMAC, Salt

26 Jun 2016, 10:16

Hi jNizM,
First, thank you for posting this — great stuff!

My script encrypts a string with a call to your SHA512 function, which works fine. Later on in the script, I want to decrypt the encrypted string, but I don't see a function to do that. Is there one? Thanks, Joe
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Hash from string, hex or file, HMAC, Salt

27 Jun 2016, 02:04

[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
JoeWinograd
Posts: 2206
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Hash from string, hex or file, HMAC, Salt

27 Jun 2016, 09:59

Hashing != En-/Decryption
Yes, thank you! Brain cramp on my part. Sorry about that. Regards, Joe
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Hash from string, hex or file, HMAC, Salt

28 Sep 2016, 06:22

New updates will be shared here => CNG (Cryptography API: Next Generation)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 65 guests