1 // Copyright 2020-present 650 Industries. All rights reserved.
2 
3 import Foundation
4 import CommonCrypto
5 
6 @objc
7 public extension NSString {
hexEncodedSHA256null8   @objc func hexEncodedSHA256() -> String {
9     let digest = (self as String).data(using:String.Encoding.utf8)!.sha256()
10     return digest.reduce("") { $0 + String(format: "%02x", $1) }
11   }
12 }
13 
14 public extension Data {
sha256null15   func sha256() -> Data {
16     var digest = Data(count: Int(CC_SHA256_DIGEST_LENGTH))
17     withUnsafeBytes { bytes in
18       digest.withUnsafeMutableBytes { mutableBytes in
19         _ = CC_SHA256(bytes.baseAddress, CC_LONG(count), mutableBytes.bindMemory(to: UInt8.self).baseAddress)
20       }
21     }
22     return digest
23   }
24 }
25