30 lines
655 B
Swift
30 lines
655 B
Swift
|
//
|
||
|
// DigestHelper.swift
|
||
|
// PSCB-OOS-iOS
|
||
|
//
|
||
|
// Created by Antonov Ilia on 28.10.2020.
|
||
|
//
|
||
|
|
||
|
import Foundation
|
||
|
import CommonCrypto
|
||
|
|
||
|
public struct DigestHelper {
|
||
|
|
||
|
public static func sha256String(_ string: String) -> String {
|
||
|
let data = string.data(using: .utf8)!
|
||
|
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
|
||
|
var sign = ""
|
||
|
|
||
|
data.withUnsafeBytes {
|
||
|
_ = CC_SHA256($0.baseAddress, CC_LONG(data.count), &hash)
|
||
|
}
|
||
|
|
||
|
for byte in hash {
|
||
|
sign += String(format: "%02x", UInt8(byte))
|
||
|
}
|
||
|
|
||
|
return sign.lowercased()
|
||
|
}
|
||
|
|
||
|
}
|