I´m trying to encode a String in SHA256. I know how the output should look like. This I´ve got from a working Java:
string = "1234nonce=1234"
SHA256 Hash from string in Java:
string.digest() =
[-29, -80, -60, 66, -104, -4, 28, 20, -102, -5, -12, -56, -103, 111,
-71, 36, 39, -82, 65, -28, 100, -101, -109, 76, -92, -107, -103, 27,
120, 82, -72, 85]
Now I need to get the same result in Swift:
func sha256() {
let string = "1234nonce=1234"
let intArray = [UInt8](string.utf8)
let data = Data(bytes: intArray)
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA256($0, CC_LONG(data.count), &hash)
}
print(hash)
return Data(bytes: hash)
}
But the result looks like this:
[218, 207, 142, 73, 87, 235, 210, 73, 201, 67, 19, 33, 57, 146, 69, 48, 51, 56, 0, 212, 172, 114, 118, 31, 102, 19, 175, 51, 153, 230, 143, 67]
How the first result is generated in Java, you can see here in the last answer.
(There, nonce + data
corrsponds to "1234nonce=1234"
)
I´m wondering if there is something I´m doing fundamentally wrong. Any kind of help is much appreciated.