-3

How can I get SHA1 hash in C# equivalent to produced by site http://www.sha1-online.com/

my sample string is

"20150819100015.test.1002-4-2015.978.GBP"

its hash string is

"caed6ade209e95ad973cce8def473f1e39e75c0b"

TFrost
  • 769
  • 2
  • 12
  • 31

1 Answers1

5

NOTE: Updated my answer to be more specific to the question at hand upon reflection to comments.

Hashes are computer over byte arrays and since the byte array representation of a string is dependent upon the text encoding you're using it may not be possible to answer your question. This is because we do not know exactly what text encoding http://www.sha1-online.com uses.

I've made an assumption below that is using an UTF8 encoding, so whilst my sample code below will produce the hash your specified in the question with the corresponding input, it may not always produce the same hash as http://www.sha1-online.com for other given inputs.

The .NET Framework provides 3 class implementations of SHA1 namely SHA1Cng, SHA1CryptoServiceProvider and SHA1Managed.

The main difference between these implementations is dicussed here Which one to use: Managed vs. NonManaged hashing algorithms, but I have used to SHA1Managed in the code snippet below as it's implemented entirely in managed code and so whilst slower, should potentially be more portable to different platforms.

The output of a hash is also an array of bytes and so to turn the hash into a text representation I have converted the bytes to their hexadecimal representation and concatenated them as this appears to be the representation that http://www.sha1-online.com chooses to use.

static void Main(string[] args)
{
    var sha1 = new System.Security.Cryptography.SHA1Managed();
    var plaintextBytes = Encoding.UTF8.GetBytes("20150819100015.test.1002-4-2015.978.GBP");
    var hashBytes = sha1.ComputeHash(plaintextBytes);

    var sb = new StringBuilder();
    foreach (var hashByte in hashBytes)
    {
        sb.AppendFormat("{0:x2}", hashByte);
    }

    var hashString = sb.ToString();
}
Community
  • 1
  • 1
DanL
  • 1,974
  • 14
  • 13
  • 2
    What is incorrect about this answer? Don't down vote without leaving a comment about why it is not useful. – DanL Aug 19 '15 at 09:54
  • 1
    A "here, I wrote your code" dump without any explanation whatsoever isn't considered quite a good answer. – CodeCaster Aug 19 '15 at 09:58
  • The Ops question was "How can I get SHA1 hash in C# equivalent to produced by site ". As you suggest a "here, I wrote your code" is not a good approach but it does not invalidate such an approach. I believe my approach is warranted as it's a relatively straight forward task and the Op was language specific, so such a simple code snippet would fulfil the required purposes. Had the Op tried something himself and not got the same answer (for example, because he was using the wrong encoding), then my answer would have warranted a explanation. However it conciseness is useful. – DanL Aug 19 '15 at 10:04
  • I'm not saying it is an invalid answer. If it were, it would have been flagged and deleted. I'm saying it isn't considered a _good_ answer, at least by two users as you see. You're making the assumption of UTF8 (could be any ASCII compatible encoding) and you're using the `SHA1Managed` class, you could explain that in your answer. It also seems to irk people to provide copy-pasteable code to zero-effort duplicate questions. – CodeCaster Aug 19 '15 at 10:07
  • 1
    So you don't find the answer useful, that is at least clear. It seems hypocritical to mark the answer as a duplicate where the duplicate question's answer makes the same assumptions. – DanL Aug 19 '15 at 10:17