1

I need to generate Message Digest in Swift with SHA-256 variant with SALT. I am using CryptoSwift library in Swift for all the encryption/decryption. Now I am stuck at generating Message Digest which matches with Java code in Android as below. If someone can help me out of this. Thanks in advance.

Library I am using is Swift : CryptoSwift

Java Code used for generating MD with Salt using SHA-256

public static String generateMessageDigest(String message,String salt) {
    try {
        MessageDigest msDigest = MessageDigest.getInstance("SHA-256");
        msDigest.update(salt.getBytes(StandardCharsets.UTF_8));
        byte[] digest = msDigest.digest(message.getBytes(StandardCharsets.UTF_8));
        return Base64.encodeBase64String(digest);
    } catch(NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    }
}

Note : I need MD with SALT

Dhaval H. Nena
  • 3,992
  • 1
  • 37
  • 50

1 Answers1

1

Finally, I managed to find the solution on my own and below is the answer.

func getHashedPassword(passwordToHash:String, salt:String) -> String {
    let combinedString = "\(salt)\(passwordToHash)"
    let result =  Digest.sha256(combinedString.bytes)
    return Data(bytes: result).base64EncodedString()
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Dhaval H. Nena
  • 3,992
  • 1
  • 37
  • 50