Anyone knows how to reproduce PHP hashing method hash(‘SHA512’, $value, true) in swift ? I tried to use CommonCrypto C library with this code :
extension String {
func digest(length:Int32, gen:(data: UnsafePointer<Void>, len: CC_LONG, md: UnsafeMutablePointer<UInt8>) -> UnsafeMutablePointer<UInt8>) -> String {
var cStr = [UInt8](self.utf8)
var result = [UInt8](count:Int(length), repeatedValue:0)
gen(data: &cStr, len: CC_LONG(cStr.count), md: &result)
let output = NSMutableString(capacity:Int(length))
for r in result {
output.appendFormat("%02x", r)
}
return String(output)
}
}
and used it like this :
var digest = salted.digest(CC_SHA512_DIGEST_LENGTH, gen: {(data, len, md) in CC_SHA512(data,len,md)})
But I don't get the right output
EDIT :
I have some PHP code :
echo base64_encode(hash('sha512', '8yOrBmkd', true)); // output: rlltLWeWaQCrfNTYMa0CcIs0mfLoHGAynrd+d8H65+rGAzHS/BSWsumwSmcxF9aAG9TIzXx+HOjArPyLL3herg==
And I want my swift code to output the same. The Base64 encoding is ok :
let utf8str = input.dataUsingEncoding(NSUTF8StringEncoding)!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
let base64Encoded = utf8str!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
print("Encoded: \(base64Encoded)")
But the hashing part return an output the same value as this PHP code :
echo base64_encode(hash('sha512', '8yOrBmkd', false)); // output: YWU1OTZkMmQ2Nzk2NjkwMGFiN2NkNGQ4MzFhZDAyNzA4YjM0OTlmMmU4MWM2MDMyOWViNzdlNzdjMWZhZTdlYWM2MDMzMWQyZmMxNDk2YjJlOWIwNGE2NzMxMTdkNjgwMWJkNGM4Y2Q3YzdlMWNlOGMwYWNmYzhiMmY3ODVlYWU=
A: SHA512 is a cryptographic hash. "SHA" is an acronym for "Secure Hash Algorithm". SHA512 is the strongest cryptographic hash in the SHA2 family.
The hash size for the SHA512 algorithm is 512 bits.
The Secure Hash Standard specifies five SHAs: SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512. All five of the algorithms are iterative, one-way hash functions that can process a message to produce a condensed representation called a message digest.
From https://stackoverflow.com/questions/6776050/how-long-to-brute-force-a-salted-sha-512-hash-salt-provided, to break SHA-512 with brute force strategy, it require 3,17 * 10^64 years.
Updating this with swift 4
func sha512Hex( string: String) -> String {
var digest = [UInt8](repeating: 0, count: Int(CC_SHA512_DIGEST_LENGTH))
if let data = string.data(using: String.Encoding.utf8) {
let value = data as NSData
CC_SHA512(value.bytes, CC_LONG(data.count), &digest)
}
var digestHex = ""
for index in 0..<Int(CC_SHA512_DIGEST_LENGTH) {
digestHex += String(format: "%02x", digest[index])
}
return digestHex
}
func sha512(string: String) -> [UInt8] {
var digest = [UInt8](repeating: 0, count: Int(CC_SHA512_DIGEST_LENGTH))
let data = string.data(using: String.Encoding.utf8 , allowLossyConversion: true)
let value = data! as NSData
CC_SHA512(value.bytes, CC_LONG(value.length), &digest)
return digest
}
func sha512Base64(string: String) -> String {
let digest = NSMutableData(length: Int(CC_SHA512_DIGEST_LENGTH))!
if let data = string.data(using: String.Encoding.utf8) {
let value = data as NSData
let uint8Pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: digest.length)
CC_SHA512(value.bytes, CC_LONG(data.count), uint8Pointer)
}
return digest.base64EncodedString(options: NSData.Base64EncodingOptions([]))
}
Testing
let vad = sha512Hex(string: "8yOrBmkd")
print(vad)
let digestRaw = sha512(string:"8yOrBmkd")
print("decimal array:\n\(digestRaw)")
print("hexadecimal:\n\(NSData(bytes:digestRaw, length:digestRaw.count).description)")
let digestBase64 = sha512Base64(string:"8yOrBmkd")
print("Base64:\n\(digestBase64)")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With