-2

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()
    }
}
makeser
  • 49
  • 9
  • 1
    Does this answer your question? [How to crypt string to sha1 in base64 with Swift?](https://stackoverflow.com/questions/31516961/how-to-crypt-string-to-sha1-in-base64-with-swift) – chirag90 Jan 23 '20 at 13:20
  • 1
    No, its different then my question let me more clear. – makeser Jan 23 '20 at 14:11

2 Answers2

1

You could use CryptoKit like this

import CryptoKit

let str: String = "Hello, world!"

//Get the SHA1 hash
let hash = Insecure.SHA1.hash(data: str.data(using: .utf8)!)

//Get string representation of the hash (matches hash.description)
let hashedString = hash.map({ String(format: "%02hhx", $0) }).joined()

//Get the base64 string
let encodedString = hashedString.data(using: .utf8)!.base64EncodedString()
Mark Thormann
  • 2,522
  • 1
  • 21
  • 23
  • I updated my question I already did that but its working wrong for my wish. – makeser Jan 23 '20 at 14:37
  • Hadn't seen the update but the code above also returns `MGZlZTA2MWZhYWIxMDllMjdiNzUwMTBmMmYxYTBkODI1OGJhYjdjNQ==` as I think it should. Can you please explain why you think it should return `D+4GH6qxCeJ7dQEPLxoNgli6t8U=`? Are you sure the input string producing the string you're looking for is "c[all]" and not something else? – Mark Thormann Jan 23 '20 at 15:26
  • Or maybe the string you're looking for was produced by a hash other than SHA1? SHA1 [isn't secure at all](https://arstechnica.com/information-technology/2017/02/at-deaths-door-for-years-widely-used-sha1-function-is-now-dead/) and you should be using SHA256 or higher if security matters. – Mark Thormann Jan 23 '20 at 15:28
0

On iOS 13+, you can use CryptoKit as follows:

import CryptoKit

extension String {
    func base64EncodedSHA1Hash(using encoding: Encoding = .utf8) -> String? {
        guard let data = data(using: encoding) else { return nil }
        let hash = Data(Insecure.SHA1.hash(data: data))
        return hash.base64EncodedString()
    }
}
Chris Chute
  • 3,229
  • 27
  • 18