SHA-256 Hash

Started by chrislegarth, March 24, 2015, 11:31:52 AM

Previous topic - Next topic

chrislegarth

I have a need to hash passwords with the SHA-256 algorithm.
Does anyone have any sample code that I could use?

Thanks!

td

Unfortunately, the FileDigest function does not support SHA-256.  But there are a few alternatives short of writing your own implementation of the algorithm.  The COM approach is the probably the simplest, if you happen to be using a version of Windows that has CAPICOM installed. Of course, there is always dotNet. 

Code (winbatch) Select

; COM (Capicom)  Approach.
SHA_256 = 4
objHash = Objectcreate("CAPICOM.HashedData.1")
objHash.Algorithm = SHA_256
strPassword = "*topsecret*"
strHash     = objHash.Hash(strPassword)
strHash     = objHash.Value
objHash = 0

Message(strPassword, strHash)

; CLR (dotNet) Approach.
objEncode = ObjectClrNew("System.Text.UTF8Encoding")
aBytes = objEncode.GetBytes(strPassword)

objSha256 = ObjectClrNew("System.Security.Cryptography.SHA256")
objHash   = objSha256.Create()
aData     = objHash.ComputeHash(System.Byte:aBytes)
objHash.Clear()

; Convert to a hex sting without using any intervening strings.
nMax     = ArrInfo(aData, 1); Should be 32
hData    = BinaryAllocArray(aData)
strHash2 = BinaryPeekHex(hData, 0, nMax)
BinaryFree(hData)

; Techniques don't produce the same result.
Message(strHash,strHash2)


"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

chrislegarth

TD-  You are a godsend.  You saved me so much time.  I was going down the .Net approach but could not get the code correct.
THANKS A MILLION!
Chris