Exemple #1
0
 /// <summary>
 /// Secure hash of the specified text into base 64.
 /// </summary>
 public static string Hash(string text, bool ascii = true)
 {
     byte[] textBytes = ascii ? Encoding.ASCII.GetBytes(text) : Encoding.UTF8.GetBytes(text);
     byte[] bytes     = BufferCache.Get(textBytes.Length + _salt.Length);
     Micron.CopyMemory(textBytes, bytes, textBytes.Length);
     Micron.CopyMemory(_salt, 0, bytes, textBytes.Length, _salt.Length);
     return(Convert.ToBase64String(_sha256.ComputeHash(bytes, 0, textBytes.Length + _salt.Length)));
 }
Exemple #2
0
        /// <summary>
        /// Continuing compute of a hash of the specified stream.
        /// </summary>
        protected void ComputeHashInner(Stream stream, IAction <byte[]> onComplete, byte[] buffer, int index)
        {
            if (buffer == null)
            {
                buffer = BufferCache.Get();
                index  = 0;
            }
            int  count = stream.Read(buffer, index, Global.BufferSizeLocal);
            bool last  = count != Global.BufferSizeLocal;

            _h1     = _seed;
            _length = 0;

            // read 128 bits, 16 bytes, 2 longs in eacy cycle
            while (index + 16 <= count)
            {
                ulong k1 = GetUInt64(buffer, index);
                ulong k2 = GetUInt64(buffer, index + 8);

                _length += ReadSize;
                MixBody(k1, k2);

                index += 16;
            }

            if (index + 16 == count)
            {
                BufferCache.Set(buffer);
                buffer = null;
            }
            else
            {
                if (last)
                {
                    // if the input MOD 16 != 0
                    if (index < count)
                    {
                        ProcessBytesRemaining(buffer, count - index, index);
                    }
                    onComplete.ArgA = Hash;
                    onComplete.Run();
                    return;
                }
                Micron.CopyMemory(buffer, index, buffer, 0, count - index);
                index -= count;
            }

            ManagerUpdate.Control.AddSingle(ComputeHashInner, stream, onComplete, buffer, index);
        }