[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1025
  • Last Modified:

SHA256 or SHA1 encryption in .NET??

I need a method for encrypting a string with SHA256 or SHA1 algorithm. I have read alot about it on the internet (and EE), but still can't find an easy solution to accomplish this:
string encoded = SHA256(string input);

So far I have played around with System.Security.Cryptography which has SHA1 and SHA256 but I am not sure how to create correct hex-strings with any of them...

Also I want the encryption to work with a javascript version of SHA256 or SHA1:
http://anmar.eu.org/projects/jssha2/ (SHA256)
http://pajhome.org.uk/crypt/md5/sha1src.html (SHA1)

Really need some code to accomplish this task....
0
wahooo
Asked:
wahooo
  • 13
  • 6
  • 2
  • +2
1 Solution
 
surajgupthaCommented:
0
 
JimBrandleyCommented:
Are you aware that both SHA1 and SHA256 are hash functions, and as such, unidirectional?
You can do ti this way in .Net:

using System.Security.Cryptography;

public static string GetHash(string plainText )
{
   string hashed = null;
   try
   {
      byte[] inputBytes = ASCIIEncoding.ASCII.GetBytes(plainText);
      SHA256Managed hash = new SHA256Managed();
      byte[] hashBytes = hash.ComputeHash(inputBytes);
      hashed = Convert.ToBase64String(hashBytes);
   }
   catch(Exception e)
   {
      throw new Exception("Hash failsed: " + e.Message);
   }
   return hashed;
}

Jim
0
 
gnoonCommented:
You cannot decrypt those encoded string because it's one way computation. To use it just compute on another string, then compare with the exist one.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
KPMT-TechnicianCommented:
/// <summary>
/// This is a one-way hashcode and can not be recovered.
/// </summary>
/// <example>
/// string _pass = SHA256HashString( "MyStrongPassword");
/// *** to test then password ***
/// bool flg = String.Compare( SHA256HashString( "MyStrongPassword"), _pass );
/// </example>
/// <param name="_unsecured">Raw string to be hashed.</param>
/// <returns>SHA256 hashed string based off the raw string passed in.</returns>private string SHA256HashString( string _unsecured )
{
    string _secured = String.Empty();
    try
    {
        using ( SHA256Managed _secureSHA256 = new SHA256Managed() )
        {
            _secured = Encoding.UTF8.GetString(
                _secureSHA256.ComputeHash(
                Encoding.UTF8.GetBytes( _unsecured ) ) );
        }
    }
    catch ( Exception ex )
    {
        throw new ApplicationException( "SHA256HashString failed: " + e.Message );
    }
    return _secured;
}

Robert Fidler
Springfield, Missouri
0
 
wahoooAuthor Commented:
To JimBrandley and KPMT-Technician:
I tried your code and they both produce a hashed string, but the output from them are both different from the SHA256 I use for the Javascript SHA256 encryption. E.g. the string "Username" becomes:
- FveKfWMX8QK72V/JpPP/LjJJKHaQuL2ta3gQ+Cs0rOM= (JimBrandley)
- ýýýsý      ýý#Býý&ýýýýOýý&ýý$Tý0@ (KPMT-Technician)
- e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 (javascript)

The latter being the javascript which I need the .NET C# to copy.
Do you know of any settings?
Is it Convert.ToBase64String(...) and Encoding.UTF8.GetString(..) that decides the output format?

The javascript uses ASCII (8 bits per input character) and returns a hexadecimal string og 65 characters (which equals 128bits?).

Looking forward to more advice...
0
 
JimBrandleyCommented:
I'll see what I can find. Having the input and output from your js halps a lot.

Jim
0
 
JimBrandleyCommented:
BTW - The js returns 64 nybbles (represented in hex), so that's 32 bytes, and 256 bits.

Jim
0
 
wahoooAuthor Commented:
Thanks Jim.
Really appreciate your help :)
0
 
JimBrandleyCommented:
I wrote this:
public static byte[] BytesToHex(byte[] bInput)
{
   byte[] bOutput;
   int nInputIndex = 0;
   int nOutputIndex = 0;
   byte nThisByte;

   bOutput = new byte[bInput.Length * 2];
   while (nInputIndex < bInput.Length)
   {
      nThisByte = (byte)((bInput[nInputIndex] & 0xf0) >> 4);
      if (nThisByte >= 10)
         nThisByte = (byte)((nThisByte - 10) + (byte)'A');
      else
         nThisByte += (byte)'0';
      bOutput[nOutputIndex++] = nThisByte;
      nThisByte = (byte)(bInput[nInputIndex++] & 0x0f);
      if (nThisByte >= 10)
         nThisByte = (byte)((nThisByte - 10) + (byte)'A');
      else
         nThisByte += (byte)'0';
      bOutput[nOutputIndex++] = nThisByte;
   }
   return bOutput;
}
private string HashToHex(string plainText)
{
   string hashed = null;
   char[] cEncryptedChars;
   try
   {
      byte[] inputBytes = UTF8Encoding.ASCII.GetBytes(plainText);
      SHA256Managed hash = new SHA256Managed();
      byte[] hashBytes = hash.ComputeHash(inputBytes);
      cEncryptedChars = ASCIIEncoding.ASCII.GetChars(BytesToHex(hashBytes));
      hashed = new String(cEncryptedChars);
   }
   catch (Exception e)
   {
      throw new Exception("Hash failsed: " + e.Message);
   }
   return hashed;
}

When I sent in "Username", I got back:
"E3B89E9D33F88E523083D8B4436ADCC3726C89E97FD3179A2E102D765D1B16ED"

Same length, but only the first byte matches that produced by the JS method.

Jim
0
 
JimBrandleyCommented:
I have done some searching, and have not found any home-grown implementations in C# that claim to match anything else. If you are a js programmer, you could translate the JS algorithm into C#, and post here if you need help. Otherwise, I can translate it if I find some time.

Jim
0
 
wahoooAuthor Commented:
Nice work!
Have you looked at the JS algorithm at:
http://anmar.eu.org/projects/jssha2/ 

I have used the one which is called hex_sha256(string s).
I must admit that I thought the SHA256 to be more standardized, maybe only variating in output format. Does it use some kind of seed/salt or predefined keys?
Or may it use different hashing methods?

I will be trying more myself, altering the JS some to see if I may get a match with your output.
Thanks again for all your help!
0
 
JimBrandleyCommented:
I downloaded the source, but do not have the time right now to attempt the translation.

I ran into a similar problem converting from fat client to .Net 1.0 - MD5 hash and and the encryption algorithm (I forget which one) we were using in the Win32 APIs did not produce the same results as those in the .Net library. So, I spent a lot of time studying encryption and built my own for .Net that gave the same results ans the Win32 APIs. We had to have both fat clients and web clients using the same encrypted passwords to connect to the DB.

Jim
0
 
JimBrandleyCommented:
I made a first pass at the translation last night. It generates a hash that doesn't match either the JS or C# results. I'm sure my lack of expertise in JS is the root of the issue.

So, I just got a copy of the standard from NIST. It contains clear definitions, the algorighm and samples of internal data at various stages of the process to validate the results of the efforts of the bit twiddling in the implementation. If you are interested, you can get it at:

http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf

I will make an effort to generate one in C#  that matches the standard as I get time. It will be interesting to see if the results match the MS version, the JS version, or neither.

Jim
 
0
 
KPMT-TechnicianCommented:
Jim,

Outstanding document, nice follow though on the research of the topic at hand.  I'd be interested in hearing about your results.  I'd started working on the javascript conversion but I don't have a well-enough grasp of using the bitwise operators or how they manipulate the data.
0
 
wahoooAuthor Commented:
Thanks Jim for all the good work you have done on this topic!

I only took a quick glance at the pdf-file, and by comparing the constants for SHA256 and the initial HASH value it is clear that the JS-version uses the same. Therefore I am very optimistic that a .NET-version following the document should provide the same output as the JS...

Please keep me posted on any progress.
0
 
JimBrandleyCommented:
Will do.

Jim
0
 
JimBrandleyCommented:
wahooo - I just wanted to post to let you know this task is not forgotten. I got a start on it, then got busy on other things. I will complete this as soon as I can.

Jim
0
 
JimBrandleyCommented:
wahooo - While working on mine, I found a discrepancy between the JS hash code downloaded from the website you posted, and the algorithm published by NIST.  In this code:

function core_sha256 (m, l) {
    var K = new Array(0x428A2F98,0x71374491,0xB5C0FBCF,0xE9B5DBA5,0x3956C25B,0x59F111F1,0x923F82A4,0xAB1C5ED5,0xD807AA98,0x12835B01,0x243185BE,0x550C7DC3,0x72BE5D74,0x80DEB1FE,0x9BDC06A7,0xC19BF174,0xE49B69C1,0xEFBE4786,0xFC19DC6,0x240CA1CC,0x2DE92C6F,0x4A7484AA,0x5CB0A9DC,0x76F988DA,0x983E5152,0xA831C66D,0xB00327C8,0xBF597FC7,0xC6E00BF3,0xD5A79147,0x6CA6351,0x14292967,0x27B70A85,0x2E1B2138,0x4D2C6DFC,0x53380D13,0x650A7354,0x766A0ABB,0x81C2C92E,0x92722C85,0xA2BFE8A1,0xA81A664B,0xC24B8B70,0xC76C51A3,0xD192E819,0xD6990624,0xF40E3585,0x106AA070,0x19A4C116,0x1E376C08,0x2748774C,0x34B0BCB5,0x391C0CB3,0x4ED8AA4A,0x5B9CCA4F,0x682E6FF3,0x748F82EE,0x78A5636F,0x84C87814,0x8CC70208,0x90BEFFFA,0xA4506CEB,0xBEF9A3F7,0xC67178F2);
    var HASH = new Array(0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19);
    var W = new Array(64);
    var a, b, c, d, e, f, g, h, i, j;
    var T1, T2;

    /* append padding */
    m[l >> 5] |= 0x80 << (24 - l % 32);
    m[((l + 64 >> 9) << 4) + 15] = l;

    for ( var i = 0; i<m.length; i+=16 ) {
        a = HASH[0];
        b = HASH[1];
        c = HASH[2];
        d = HASH[3];
        e = HASH[4];
        f = HASH[5];
        g = HASH[6];
        h = HASH[7];

        for ( var j = 0; j<64; j++) {
            if (j < 16) W[j] = m[j + i];
            else W[j] = safe_add(safe_add(safe_add(Gamma1256(W[j - 2]), W[j - 7]), Gamma0256(W[j - 15])), W[j - 16]);

            T1 = safe_add(safe_add(safe_add(safe_add(h, Sigma1256(e)), Ch(e, f, g)), K[j]), W[j]);
            T2 = safe_add(Sigma0256(a), Maj(a, b, c));

            h = g;
            g = f;
            f = e;
            e = safe_add(d, T1);
            d = c;
            c = b;
            b = a;
            a = safe_add(T1, T2);
        }
       
        HASH[0] = safe_add(a, HASH[0]);
        HASH[1] = safe_add(b, HASH[1]);
        HASH[2] = safe_add(c, HASH[2]);
        HASH[3] = safe_add(d, HASH[3]);
        HASH[4] = safe_add(e, HASH[4]);
        HASH[5] = safe_add(f, HASH[5]);
        HASH[6] = safe_add(g, HASH[6]);
        HASH[7] = safe_add(h, HASH[7]);
    }
    return HASH;
}

The first line of the inner loop (for ( var j = 0; j<64; j++) ) is:
            if (j < 16) W[j] = m[j + i];
According to the publiched algorithm, this should be:
            if (j < 16) W[j] = m[j];

Here's another discrepancy:
In the same website:
http://anmar.eu.org/projects/jssha2/

They have a test facility: When I used it to hash Username, I got a different result than you posted. That one matched the output from the ManagedSHA256 provider:
web site: "e3b89e9d33f88e523083d8b4436adcc3726c89e97fd3179a2e102d765d1b16ed"
.Net:        ""E3B89E9D33F88E523083D8B4436ADCC3726C89E97FD3179A2E102D765D1B16ED"

If you change the BytesToHex() method I posted to use  'a' instead of 'A', the output is identical.

When I ran the string "abc" through this website, and the managed .Net provider, both produced the same result as that ppublished by NIST.

Now, the question becomes: Where did you get:
"- e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 (javascript)"?

That seems to be the mismatch.

Jim
0
 
JimBrandleyCommented:
Since the version I translated from JS now matches both the native .Net version and NIST, do you want me to post it, or do you no longer need it? I will be out of town for the next three days. Will check when I get back.

Jim
0
 
wahoooAuthor Commented:
JimBrandley:
Hi. Your work is very impressive, and I will accept it as the solution. But may you post your solution so it is available to the network (hopefully this will be solution 2 000 000, so you get what you deserve ;)?

Thanks again for our terrific help with this!
- wahooo
0
 
JimBrandleyCommented:
Thanks. The entire file is below. I cannot participate in the contest because I have some administrative credentials as a cleanup volunteer. Maybe you will win with your next question.

Jim

using System;
using System.Collections.Generic;
using System.Text;

namespace EncryptionTest
{
      class SHA256FromJS
      {
            /// <summary>
            /// This class is a translation to C# from JS code published at:
            /// http://anmar.eu.org/projects/jssha2/ 
            /// </summary>
            #region Members
            private static int chrsz = 8;      // Bits per input character
            #endregion

            #region Public Method
            public static string hex_sha256(string s)
            {
                  return binb2hex(core_sha256(str2binb(s), s.Length * chrsz));
            }

            #endregion

            #region Private methods
            private static uint safe_add(uint x, uint y)
            {
                  uint lsw = (x & 0xFFFF) + (y & 0xFFFF);
                  uint msw = (x >> 16) + (y >> 16) + (lsw >> 16);
                  return (msw << 16) | (lsw & 0xFFFF);
            }
            private static uint S(uint x, int n)
            {
                  return ( x >> n ) | (x << (32 - n));
            }
            private static uint R(uint x, int n)
            {
                  return (x >> n);
            }
            private static uint Ch(uint x, uint y, uint z)
            {
                  return ((x & y) ^ ((~x) & z));
            }
            private static uint Maj(uint x, uint y, uint z)
            {
                  return ((x & y) ^ (x & z) ^ (y & z));
            }
            private static uint Sigma0256(uint x)
            {
                  return (S(x, 2) ^ S(x, 13) ^ S(x, 22));
            }
            private static uint Sigma1256(uint x)
            {
                  return (S(x, 6) ^ S(x, 11) ^ S(x, 25));
            }
            private static uint Gamma0256(uint x)
            {
                  return (S(x, 7) ^ S(x, 18) ^ R(x, 3));
            }
            private static uint Gamma1256(uint x)
            {
                  return (S(x, 17) ^ S(x, 19) ^ R(x, 10));
            }
            private static uint[] str2binb(string str)
            {
                  byte[] plainBytes = ASCIIEncoding.ASCII.GetBytes(str);

                  int characters = plainBytes.Length;
                  ulong textLen = (ulong)characters * (ulong)8;
                  int padLength = 512 - (int)((textLen + (ulong)65) % (ulong)512);
                  int messageLength = (int)((textLen + (ulong)65 + (ulong)padLength) / (ulong)32);

                  uint[] bin = new uint[messageLength];
                  Array.Clear(bin, 0, bin.Length);      // Initialize to all zeros

                  uint mask = (uint)((1 << chrsz) - 1);

                  for (int i = 0; i < plainBytes.Length * chrsz; i += chrsz)
                  {
                        bin[i >> 5] |= (plainBytes[i / chrsz] & mask) << (24 - i % 32);
                  }
                  return bin;
            }
            private static string binb2hex(uint[] binarray)
            {
                  string hex_tab = "0123456789abcdef";
                  StringBuilder result = new StringBuilder(64);
                  char c1;
                  char c2;
                  for (int i = 0; i < binarray.Length * 4; i++)
                  {
                        c1 = hex_tab[(int)((binarray[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xF)];
                        c2 = hex_tab[(int)((binarray[i >> 2] >> ((3 - i % 4) * 8)) & 0xF)];
                        result.Append(c1);
                        result.Append(c2);
                  }
                  return result.ToString();
            }
            private static uint[] core_sha256(uint[] m, int l)
            {
                  uint[] K = new uint[] { 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
                                                  0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
                                                  0xE49B69C1, 0xEFBE4786, 0xFC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
                                                  0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x6CA6351, 0x14292967,
                                                  0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
                                                  0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
                                                  0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
                                                  0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2 };
                  uint[] HASH = new uint[] { 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19 };
                  uint[] W = new uint[64];
                  uint a, b, c, d, e, f, g, h;
                  uint T1, T2;

                  // Append padding
                  m[l >> 5] |= (uint)(0x80 << (24 - l % 32));
                  m[((l + 64 >> 9) << 4) + 15] = (uint)l;
                  for (int i = 0; i < m.Length; i += 16)
                  {
                        a = HASH[0];
                        b = HASH[1];
                        c = HASH[2];
                        d = HASH[3];
                        e = HASH[4];
                        f = HASH[5];
                        g = HASH[6];
                        h = HASH[7];
                        for (int j = 0; j < 64; j++)
                        {
                              if (j < 16)
                                    W[j] = m[j]; // Fixed bug here. Was: W[j] = m[j + i];
                              else
                                    W[j] = safe_add(safe_add(safe_add(Gamma1256(W[j - 2]), W[j - 7]), Gamma0256(W[j - 15])), W[j - 16]);
                              T1 = safe_add(safe_add(safe_add(safe_add(h, Sigma1256(e)), Ch(e, f, g)), K[j]), W[j]);
                              T2 = safe_add(Sigma0256(a), Maj(a, b, c));
                              h = g;
                              g = f;
                              f = e;
                              e = safe_add(d, T1);
                              d = c;
                              c = b;
                              b = a;
                              a = safe_add(T1, T2);
                        }
                        HASH[0] = safe_add(a, HASH[0]);
                        HASH[1] = safe_add(b, HASH[1]);
                        HASH[2] = safe_add(c, HASH[2]);
                        HASH[3] = safe_add(d, HASH[3]);
                        HASH[4] = safe_add(e, HASH[4]);
                        HASH[5] = safe_add(f, HASH[5]);
                        HASH[6] = safe_add(g, HASH[6]);
                        HASH[7] = safe_add(h, HASH[7]);
                  }
                  return HASH;
            }
            #endregion

            #region Algorithm from http://anmar.eu.org/projects/jssha2/
            /*
            Note: All variables are unsigned 32 bits and wrap modulo 232 when calculating

            Initialize variables
            (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
            h0 := 0x6a09e667
            h1 := 0xbb67ae85
            h2 := 0x3c6ef372
            h3 := 0xa54ff53a
            h4 := 0x510e527f
            h5 := 0x9b05688c
            h6 := 0x1f83d9ab
            h7 := 0x5be0cd19

            Initialize table of round constants
            (first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311):
            k[0..63] :=
               0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
               0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
               0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
               0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
               0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
               0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
               0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
               0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2

            Pre-processing:
            append the bit '1' to the message
            append k bits '0', where k is the minimum number >= 0 such that the resulting message
                  length (in bits) is congruent to 448 (mod 512)
            append length of message (before pre-processing), in bits, as 64-bit big-endian integer

            Process the message in successive 512-bit chunks:
            break message into 512-bit chunks
            for each chunk
                  break chunk into sixteen 32-bit big-endian words w[0..15]

                  Extend the sixteen 32-bit words into sixty-four 32-bit words:
                  for i from 16 to 63
                        s0 := (w[i-15] rightrotate 7) xor (w[i-15] rightrotate 18) xor (w[i-15] rightshift 3)
                        s1 := (w[i-2] rightrotate 17) xor (w[i-2] rightrotate 19) xor (w[i-2] rightshift 10)
                        w[i] := w[i-16] + s0 + w[i-7] + s1

                  Initialize hash value for this chunk:
                  a := h0
                  b := h1
                  c := h2
                  d := h3
                  e := h4
                  f := h5
                  g := h6
                  h := h7

                  Main loop:
                  for i from 0 to 63
                        s0 := (a rightrotate 2) xor (a rightrotate 13) xor (a rightrotate 22)
                        maj := (a and b) xor (a and c) xor (b and c)
                        t2 := s0 + maj
                        s1 := (e rightrotate 6) xor (e rightrotate 11) xor (e rightrotate 25)
                        ch := (e and f) xor ((not e) and g)
                        t1 := h + s1 + ch + k[i] + w[i]

                        h := g
                        g := f
                        f := e
                        e := d + t1
                        d := c
                        c := b
                        b := a
                        a := t1 + t2

                  Add this chunk's hash to result so far:
                  h0 := h0 + a
                  h1 := h1 + b
                  h2 := h2 + c
                  h3 := h3 + d
                  h4 := h4 + e
                  h5 := h5 + f
                  h6 := h6 + g
                  h7 := h7 + h

            Produce the final hash value (big-endian):
            digest = hash = h0 append h1 append h2 append h3 append h4 append h5 append h6 append h7
            */
            #endregion


            #region Original JS Source
            /*

            var chrsz = 8;  // bits per input character. 8 - ASCII; 16 - Unicode  
            function safe_add (x, y) {
              var lsw = (x & 0xFFFF) + (y & 0xFFFF);
              var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
              return (msw << 16) | (lsw & 0xFFFF);
            }
            function S (X, n) {return ( X >>> n ) | (X << (32 - n));}
            function R (X, n) {return ( X >>> n );}
            function Ch(x, y, z) {return ((x & y) ^ ((~x) & z));}
            function Maj(x, y, z) {return ((x & y) ^ (x & z) ^ (y & z));}
            function Sigma0256(x) {return (S(x, 2) ^ S(x, 13) ^ S(x, 22));}
            function Sigma1256(x) {return (S(x, 6) ^ S(x, 11) ^ S(x, 25));}
            function Gamma0256(x) {return (S(x, 7) ^ S(x, 18) ^ R(x, 3));}
            function Gamma1256(x) {return (S(x, 17) ^ S(x, 19) ^ R(x, 10));}
            function core_sha256 (m, l) {
                  var K = new Array(0x428A2F98,0x71374491,0xB5C0FBCF,0xE9B5DBA5,0x3956C25B,0x59F111F1,0x923F82A4,0xAB1C5ED5,0xD807AA98,0x12835B01,0x243185BE,0x550C7DC3,0x72BE5D74,0x80DEB1FE,0x9BDC06A7,0xC19BF174,0xE49B69C1,0xEFBE4786,0xFC19DC6,0x240CA1CC,0x2DE92C6F,0x4A7484AA,0x5CB0A9DC,0x76F988DA,0x983E5152,0xA831C66D,0xB00327C8,0xBF597FC7,0xC6E00BF3,0xD5A79147,0x6CA6351,0x14292967,0x27B70A85,0x2E1B2138,0x4D2C6DFC,0x53380D13,0x650A7354,0x766A0ABB,0x81C2C92E,0x92722C85,0xA2BFE8A1,0xA81A664B,0xC24B8B70,0xC76C51A3,0xD192E819,0xD6990624,0xF40E3585,0x106AA070,0x19A4C116,0x1E376C08,0x2748774C,0x34B0BCB5,0x391C0CB3,0x4ED8AA4A,0x5B9CCA4F,0x682E6FF3,0x748F82EE,0x78A5636F,0x84C87814,0x8CC70208,0x90BEFFFA,0xA4506CEB,0xBEF9A3F7,0xC67178F2);
                  var HASH = new Array(0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19);
                  var W = new Array(64);
                  var a, b, c, d, e, f, g, h, i, j;
                  var T1, T2;
                  // append padding
                  m[l >> 5] |= 0x80 << (24 - l % 32);
                  m[((l + 64 >> 9) << 4) + 15] = l;
                  for ( var i = 0; i<m.length; i+=16 ) {
                        a = HASH[0]; b = HASH[1]; c = HASH[2]; d = HASH[3]; e = HASH[4]; f = HASH[5]; g = HASH[6]; h = HASH[7];
                        for ( var j = 0; j<64; j++)
                        {
                              if (j < 16)
                                     W[j] = m[j + i];  // Not - this is a bug. should be: W[j] = m[j];
                              else
                                    W[j] = safe_add(safe_add(safe_add(Gamma1256(W[j - 2]), W[j - 7]), Gamma0256(W[j - 15])), W[j - 16]);
                              T1 = safe_add(safe_add(safe_add(safe_add(h, Sigma1256(e)), Ch(e, f, g)), K[j]), W[j]);
                              T2 = safe_add(Sigma0256(a), Maj(a, b, c));
                              h = g; g = f; f = e; e = safe_add(d, T1); d = c; c = b; b = a; a = safe_add(T1, T2);
                        }
                        HASH[0] = safe_add(a, HASH[0]);
                        HASH[1] = safe_add(b, HASH[1]);
                        HASH[2] = safe_add(c, HASH[2]);
                        HASH[3] = safe_add(d, HASH[3]);
                        HASH[4] = safe_add(e, HASH[4]);
                        HASH[5] = safe_add(f, HASH[5]);
                        HASH[6] = safe_add(g, HASH[6]);
                        HASH[7] = safe_add(h, HASH[7]);
                  }
                  return HASH;
            }
            function str2binb (str) {
              var bin = Array();
              var mask = (1 << chrsz) - 1;
              for(var i = 0; i < str.length * chrsz; i += chrsz)
                  bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (24 - i%32);
              return bin;
            }
            function binb2hex (binarray) {
              var hexcase = 0; // hex output format. 0 - lowercase; 1 - uppercase
              var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
              var str = "";
              for (var i = 0; i < binarray.length * 4; i++) {
                  str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) + hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8  )) & 0xF);
              }
              return str;
            }
            function hex_sha256(s){return binb2hex(core_sha256(str2binb(s),s.length * chrsz));}

*/
            #endregion
      }
}
0
 
wahoooAuthor Commented:
Wow!
Worked without any trouble!

- wahoo
0
 
JimBrandleyCommented:
Glad that helped. Good luck.

Jim
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 13
  • 6
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now