Level up your Box knowledge with brand new learning paths on Box University. Visit training.box.com to get started

How is sha1 hash generated

New post

Comments

1 comment

  • Michael Anthony Alabastro

    //This is how I do it in C#. You may have to google how the SHA1Managed object does it. It returns the same sha1 string as the Box sha1.

    //Cut-and-pasting relevant snippets from what I have...

    using SSC = System.Security.Cryptography;
    using SI = System.IO;

    public static string GetSha1HashOfFile(string pathToFile)
    {
        using (SSC.SHA1Managed sha1 = new SSC.SHA1Managed())
        {
            using (SI.FileStream stream = SI.File.OpenRead(pathToFile))
            {
                return GetSha1Hash(sha1, stream);
            }
        }
    }

    public static string GetSha1Hash(SSC.SHA1Managed sha1, SI.Stream stream)
    {
        byte[] hash = sha1.ComputeHash(stream);
        return HashByteArrayToString(hash);
    }

    public static string HashByteArrayToString(byte[] hashByteArray)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder(hashByteArray.Length * 2);
        foreach (byte b in hashByteArray)
        {
            // can be "x2" if you want lowercase
            sb.Append(b.ToString("X2"));
        }
        return sb.ToString();
    }

    0
    Comment actions Permalink

Please sign in to leave a comment.