0

I am using OpenSSL Library compiled for IOS project. So I need to calculate ripemd160 hash. From RIPEMD160 function, I got 20 bytes array, and not the hash. But I need hash like this: 6c9814cf2a93131c8d3263158896e786de7a3f21. How I can achieve this? Please help who knows C and Swift. Thanks.

Update: Thanks @MartinR and his comment. Accepted answer helped. Here stackoverflow.com/q/39075043/1187415

        let publicKey = "04981891480BC21B0AFE20FAF8F49FB1618E80769D0956A730B8409E856CB9512B4778CB346C211065050E008412C29B0474EA56AEA3761DBD75655B4FBD252E39"

        var md = [UInt8](repeating: 0, count: Int(RIPEMD160_DIGEST_LENGTH))
        let PubKeyBuf = [UInt8](publicKey.utf8)
        
        let sha256Desc = SHA256.hash(data: PubKeyBuf).description
        let startIndex = sha256Desc.index(sha256Desc.endIndex, offsetBy: -64)
        let SHA256 = String(sha256Desc[startIndex..<sha256Desc.endIndex])
        var d = [UInt8](SHA256.utf8)

        RIPEMD160(&d,  Int(RIPEMD160_DIGEST_LENGTH),  &md)
        print(md) // returns [219, 95, 117, 159, 78, 53, 169, 215, 52, 42, 30, 65, 216, 160, 91, 92, 59, 233, 33, 76]```
  • Not an answer to your question, but note that there are implementation of RIPEMD-160 in pure Swift, compare https://stackoverflow.com/q/43091858/1187415. – Martin R May 22 '21 at 19:25
  • 1
    Here https://stackoverflow.com/q/39075043/1187415 you can find various methods to convert `Data` to a hex string. With minor modifications, these work on an `[UInt8]` array as well. – Martin R May 22 '21 at 19:33
  • Thanks, I will look at it and post back if it helped. – Nickey Production May 24 '21 at 07:40
  • 1
    @MartinR stackoverflow.com/q/39075043/1187415 Accepted answer here helped me, thanks BIG! – Nickey Production May 24 '21 at 07:52

0 Answers0