Message Digest in swift

753 Views Asked by At

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

1

There are 1 best solutions below

0
On

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()
}