Skip to content

How can I SHA512 a string in C#? #6

@marcialwushu

Description

@marcialwushu

https://stackoverflow.com/questions/11367727/how-can-i-sha512-a-string-in-c

byte[] data = new byte[DATA_SIZE];
byte[] result;
SHA512 shaM = new SHA512Managed();
result = shaM.ComputeHash(data);

If you need the HMAC-version (to add authentication to the hash)

public static byte[] HmacSha512(string text, string key)
{
    byte[] bytes = Encoding.UTF8.GetBytes(text);

    var hmac = new Org.BouncyCastle.Crypto.Macs.HMac(new Org.BouncyCastle.Crypto.Digests.Sha512Digest());
    hmac.Init(new Org.BouncyCastle.Crypto.Parameters.KeyParameter(System.Text.Encoding.UTF8.GetBytes(key)));

    byte[] result = new byte[hmac.GetMacSize()];
    hmac.BlockUpdate(bytes, 0, bytes.Length);
    hmac.DoFinal(result, 0);

    return result;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions