Reputation: 411
func SHA256() -> String {
let data = self.data(using: String.Encoding.utf8)
let res = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH))
CC_SHA256((data! as NSData).bytes, CC_LONG(data!.count), UnsafeMutablePointer(res!.mutableBytes))
let hashedString = "\(res!)".replacingOccurrences(of: "", with: "").replacingOccurrences(of: " ", with: "")
let badchar: CharacterSet = CharacterSet(charactersIn: "\"<\",\">\"")
let cleanedstring: String = (hashedString.components(separatedBy: badchar) as NSArray).componentsJoined(by: "")
return cleanedstring
}
I am using this function to encrypt strings it was working fine in swift 2,
now its not working in swift 3.0
Upvotes: 2
Views: 5452
Reputation: 3496
Perfect solution Swift 3+:
extension String {
// MARK: - SHA256
func get_sha256_String() -> String {
guard let data = self.data(using: .utf8) else {
print("Data not available")
return ""
}
return getHexString(fromData: digest(input: data as NSData))
}
private func digest(input : NSData) -> NSData {
let digestLength = Int(CC_SHA256_DIGEST_LENGTH)
var hashValue = [UInt8](repeating: 0, count: digestLength)
CC_SHA256(input.bytes, UInt32(input.length), &hashValue)
return NSData(bytes: hashValue, length: digestLength)
}
private func getHexString(fromData data: NSData) -> String {
var bytes = [UInt8](repeating: 0, count: data.length)
data.getBytes(&bytes, length: data.length)
var hexString = ""
for byte in bytes {
hexString += String(format:"%02x", UInt8(byte))
}
return hexString
}
}
How to use:
let desiredSHA256 = "yourString".get_sha256_String()
Upvotes: 8
Reputation: 112857
func sha256(string: String) -> Data? {
guard let messageData = string.data(using:String.Encoding.utf8) else { return nil; }
var digestData = Data(count: Int(CC_SHA256_DIGEST_LENGTH))
_ = digestData.withUnsafeMutableBytes {digestBytes in
messageData.withUnsafeBytes {messageBytes in
CC_SHA256(messageBytes, CC_LONG(messageData.count), digestBytes)
}
}
return digestData
}
Example:
let testString = "sha me"
print("testString: \(testString)")
let shaData = sha256(string: testString)
let shaHex = shaData!.map { String(format: "%02hhx", $0) }.joined()
print("shaHex: \(shaHex)")
Output:
testString: sha me
shaData: a60e0eee 30a3a4f1 c4f8b93f 16ad22cb 0339447b 1653f331 edbda55f eee00789
What is new is the .withUnsafeMutableBytes
closure.
Upvotes: 5
Reputation: 411
func SHA256() -> String {
let data = self.data(using: String.Encoding.utf8)
let res = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH))
CC_SHA256(((data! as NSData)).bytes, CC_LONG(data!.count), res?.mutableBytes.assumingMemoryBound(to: UInt8.self))
let hashedString = "\(res!)".replacingOccurrences(of: "", with: "").replacingOccurrences(of: " ", with: "")
let badchar: CharacterSet = CharacterSet(charactersIn: "\"<\",\">\"")
let cleanedstring: String = (hashedString.components(separatedBy: badchar) as NSArray).componentsJoined(by: "")
return cleanedstring
}
Replaced CC_SHA256((data! as NSData).bytes, CC_LONG(data!.count), UnsafeMutablePointer(res!.mutableBytes))
Upvotes: 4