How to pass SHA512 string to Swift?

I am building a social media application and I need some help with the password string encoding for SHA512 in Swift. I found the CryptoSwift library on GitHub, but it's hard for me to load it into my Swift project and link it to the project files. Does anyone know how to do this relatively easily? Thanks in advance, Kyle

+6
source share
4 answers

Solution for Swift 3 :

extension String { func sha512() -> String { let data = self.data(using: .utf8)! var digest = [UInt8](repeating: 0, count: Int(CC_SHA512_DIGEST_LENGTH)) data.withUnsafeBytes({ _ = CC_SHA512($0, CC_LONG(data.count), &digest) }) return digest.map({ String(format: "%02hhx", $0) }).joined(separator: "") } } 
+7
source

Swift 3

 func sha512() -> String { let data = self.data(using: .utf8)! var digest = [UInt8](repeating: 0, count: Int(CC_SHA512_DIGEST_LENGTH)) data.withUnsafeBytes({ _ = CC_SHA512($0, CC_LONG(data.count), &digest) }) return digest.map({ String(format: "%02hhx", $0) }).joined(separator: "") } 

Swift 2.3

 func sha512() -> String { let data = self.dataUsingEncoding(NSUTF8StringEncoding)! var digest = [UInt8](count:Int(CC_SHA512_DIGEST_LENGTH), repeatedValue: 0) CC_SHA512(data.bytes, CC_LONG(data.length), &digest) let hexBytes = digest.map { String(format: "%02hhx", $0) } return hexBytes.joinWithSeparator("") } 
+2
source

I find the whole answer in order, but if we have to have a true universal solution, I think we need to increase its level.

CC_LONG is just UInt32 and will not support really large data structures.

This is my solution in Swift 3 :

First we create the foundation:

 struct Sha512 { let context = UnsafeMutablePointer<CC_SHA512_CTX>.allocate(capacity:1) init() { CC_SHA512_Init(context) } func update(data: Data) { data.withUnsafeBytes { (bytes: UnsafePointer<Int8>) -> Void in let end = bytes.advanced(by: data.count) for f in sequence(first: bytes, next: { $0.advanced(by: Int(CC_LONG.max)) }).prefix(while: { (current) -> Bool in current < end}) { _ = CC_SHA512_Update(context, f, CC_LONG(Swift.min(f.distance(to: end), Int(CC_LONG.max)))) } } } func final() -> Data { var digest = [UInt8](repeating: 0, count:Int(CC_SHA512_DIGEST_LENGTH)) CC_SHA512_Final(&digest, context) return Data(bytes: digest) } } 

For convenience, we are doing an extension for Data :

 extension Data { func sha512() -> Data { let s = Sha512() s.update(data: self) return s.final() } } 

And the last extension for String :

 extension String { func sha512() -> Data { return self.data(using: .utf8)!.sha512() } } 

This solution can be used for Sha256, MD5, etc., to get good real universal solutions with Apple CommonCrypto.

+2
source

You need to import the CommonCrypto C library. You cannot simply import CommonCrypto into your fast file, because it is not a standalone module.

If you have a header file with a bridge, you're in luck! Just add this to this file.

 #import <CommonCrypto/CommonCrypto.h> 

There are several articles about different ways of doing this.

Then you can use this piece of code so that sha512 is available for any line of your choice:

 extension String { public var sha512: String { let data = self.data(using: .utf8) ?? Data() var digest = [UInt8](repeating: 0, count: Int(CC_SHA512_DIGEST_LENGTH)) data.withUnsafeBytes({ _ = CC_SHA512($0, CC_LONG(data.count), &digest) }) return digest.map({ String(format: "%02hhx", $0) }).joined(separator: "") } } 
+1
source

Source: https://habr.com/ru/post/987358/


All Articles