Hash from string, hex or file, HMAC, Salt

Post your working scripts, libraries and tools
User avatar
jNizM
Posts: 2661
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

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

16 May 2014, 01:47

[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
jNizM
Posts: 2661
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
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] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
jNizM
Posts: 2661
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
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] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
JoeWinograd
Posts: 1592
Joined: 10 Feb 2014, 20:00

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: 2661
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

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

27 Jun 2016, 02:04

[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
JoeWinograd
Posts: 1592
Joined: 10 Feb 2014, 20:00

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: 2661
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
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] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 29 guests