I have to convert string to sha1 and then use base64. Simply base64_encode(sha1(My_String))
. I want to do that but I can't fix it correctly. I can convert SHA1 with that code: let firstTry = SHA1.hash(from: "call")
but when I try to make it in base64 it gave error which is say string not allowed. How can I convert base64?
Thanks for your attention.
I try to conver c[all] to sha1 with that code :
let str = "c[all]"
let den3 = str.sha1()
its working good and return correct which is : 0fee061faab109e27b75010f2f1a0d8258bab7c5
And when I add let den3 = str.sha1().toBase64()
I get MGZlZTA2MWZhYWIxMDllMjdiNzUwMTBmMmYxYTBkODI1OGJhYjdjNQ== which is wrong actually I need to get that: D+4GH6qxCeJ7dQEPLxoNgli6t8U=
Where is my issue?
Here my extensions
extension String {
func sha1() -> String {
let data = Data(self.utf8)
var digest = [UInt8](repeating: 0, count:Int(CC_SHA1_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA1($0.baseAddress, CC_LONG(data.count), &digest)
}
let hexBytes = digest.map { String(format: "%02hhx", $0) }
return hexBytes.joined()
}
func toBase64() -> String {
return Data(self.utf8).base64EncodedString()
}
}