Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SHA256 in swift

Tags:

swift

sha256

I want to use sha256 in my project, but I had some troubles rewriting objC code to swift code. Help me please. I used this answer: How can I compute a SHA-2 (ideally SHA 256 or SHA 512) hash in iOS?

Here's my code

var hash : [CUnsignedChar] CC_SHA256(data.bytes, data.length, hash) var res : NSData = NSData.dataWithBytes(hash, length: CC_SHA256_DIGEST_LENGTH) 

it gives me error everything because swift cannot convert Int to CC_LONG, for example.

like image 991
Yury Alexandrov Avatar asked Aug 19 '14 16:08

Yury Alexandrov


People also ask

What is SHA 256 hash used for?

SHA-256 is used in some of the most popular authentication and encryption protocols, including SSL, TLS, IPsec, SSH, and PGP. In Unix and Linux, SHA-256 is used for secure password hashing. Cryptocurrencies such as Bitcoin use SHA-256 for verifying transactions.

How is SHA 256 calculated?

For SHA-256 these are calculated from the first 8 primes. These always remain the same for any message. The primes are firstly square rooted and then taken to the modulus 1. The result is then multiplied by 16⁸ and rounded down to the nearest integer.

What is MD5 and SHA256?

Both MD5 and SHA256 are used as hashing algorithms. They take an input file and generate an output which can be of 256/128-bit size. This output represents a checksum or hash value. As, collisions are very rare between hash values, so no encryption takes place.

Is SHA 512 better than SHA256?

The reason why SHA-512 is faster than SHA-256 on 64-bit machines is that has 37.5% less rounds per byte (80 rounds operating on 128 byte blocks) compared to SHA- 256 (64 rounds operating on 64 byte blocks), where the operations use 64-bit integer arithmetic.


1 Answers

You have to convert explicitly between Int and CC_LONG, because Swift does not do implicit conversions, as in (Objective-)C.

You also have to define hash as an array of the required size.

func sha256(data : NSData) -> NSData {     var hash = [UInt8](count: Int(CC_SHA256_DIGEST_LENGTH), repeatedValue: 0)     CC_SHA256(data.bytes, CC_LONG(data.length), &hash)     let res = NSData(bytes: hash, length: Int(CC_SHA256_DIGEST_LENGTH))     return res } 

Alternatively, you can use NSMutableData to allocate the needed buffer:

func sha256(data : NSData) -> NSData {     let res = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH))     CC_SHA256(data.bytes, CC_LONG(data.length), UnsafeMutablePointer(res.mutableBytes))     return res } 

Update for Swift 3 and 4:

func sha256(data : Data) -> Data {     var hash = [UInt8](repeating: 0,  count: Int(CC_SHA256_DIGEST_LENGTH))     data.withUnsafeBytes {         _ = CC_SHA256($0, CC_LONG(data.count), &hash)     }     return Data(bytes: hash) } 

Update for Swift 5:

func sha256(data : Data) -> Data {     var hash = [UInt8](repeating: 0,  count: Int(CC_SHA256_DIGEST_LENGTH))     data.withUnsafeBytes {         _ = CC_SHA256($0.baseAddress, CC_LONG(data.count), &hash)     }     return Data(hash) } 
like image 89
Martin R Avatar answered Sep 18 '22 04:09

Martin R