Search code examples
.net-corehashsha

can I use sha1,sha256 lib generated string as a hash key


I'm looking for a way to create a hash key based on a few string properties of a class. I was thinking I could use the sha1 or sha256 libs to generate the value for the key. But I'm wondering if I store that value in the DB will another app or different machine or different versions .net and .netcore be able to generate the same hash given the same string properties? A simple example of generating the hash bellow:

 public class Lead
{
    public Lead(string firstname, string lastname, string phoneNumber)
    {
        this.firstname = firstname;
        this.lastname = lastname;
        phonenumber = phoneNumber;
        this.hash = GetHash($"{firstname}{lastname}{phoneNumber}");
    }

    private string GetHash(string key)
    {
        using var sha = new System.Security.Cryptography.SHA256Managed();
        byte[] textData = System.Text.Encoding.UTF8.GetBytes(key);
        byte[] hash = sha.ComputeHash(textData);
        return BitConverter.ToString(hash).Replace("-", string.Empty);
    }
    public string firstname;
    public string lastname;
    public string phonenumber;
    public string hash;
}

Solution

  • The answer is yes, I was also able to encrypt in .net and for kicks decrypt the value in rails.