SHA512 encryption - XAMARIN

Asked

Viewed 379 times

2

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

The idea was to do something like:

public string Encripty(DateTime dataAtual, string stringQueQueroEncriptar){

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

}

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;
    }
  • So, I’ve tried this but in PCL (Portable Class Labrary) does not have the assemblys related to this encryption, and if I try to download by Nuguet, also does not accept. The only package I was able to install by Nuguet was Pclcrypto, but I was unable to create an encryption in the model I reported by entering a secret key, the examples I found will only inform the content to be encrypted.

  • I edited the post. Take a look and see if it works now.

  • It was not expensive, I copied and pasted, I tried to import Assembly Mono.Android, unsuccessfully. does not recognize this code. Thanks for the effort, I keep trying here...

1

I found/ - Installing from Nuguet Pclcrypto(when installing, Assembly Validation has to be appearing tbm in the reference list, or 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();
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.