Cryptography SHA512 - XAMARIN

2

Good afternoon, I'm trying to encrypt a string with SHA512, in project [PCL], I'm not getting it, can anyone show an example of how to do it?

The idea was to do something like:

public string Encripty(DateTime dataAtual, string stringQueQueroEncriptar){

return data = SHA512.secretKey("dataAtual").Hash("stringQueQueroEncriptar");

}
    
asked by anonymous 03.03.2017 / 18:29

2 answers

1

According to this post , the namespace System.Security.Cryptography is not compatible with PCL, but Mono yes.

Try something like:

protected override byte[] ComputeHash(byte[] data)
    {
        var input = CryptographicBuffer.CreateFromByteArray(data);

        var hasher = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512);
        var hashedBuf = hasher.HashData(input);

        byte[] result = new byte[hashedBuf.Length];
        CryptographicBuffer.CopyToByteArray(hashedBuf, out result);
        return result;
    }
    
03.03.2017 / 18:53
1

I thought \ o / - Installing the PCLCrypto from Nuguet (when installing, the Validation assembly must be appearing in the reference list, otherwise install manually by entering the PCLCrypto folder). just send that code there and success!

public string CreateHash(string date, string userId) {



            IMacAlgorithmProvider mac = WinRTCrypto.MacAlgorithmProvider.OpenAlgorithm(MacAlgorithm.HmacSha512);

            byte[] keyMaterial = WinRTCrypto.CryptographicBuffer.ConvertStringToBinary(date, crypto.Encode());

            ICryptographicKey cryptoKey = mac.CreateKey(keyMaterial);

            byte[] hash = WinRTCrypto.CryptographicEngine.Sign(cryptoKey, WinRTCrypto.CryptographicBuffer.ConvertStringToBinary(userId, Encoding.UTF8));

            StringBuilder hashHMAC = new StringBuilder();
            for (int i = 0; i < hash.Length; i++) {
                hashHMAC.Append(hash[i].ToString("X2"));
            }

            return hashHMAC.ToString();
        }
    
03.03.2017 / 21:48