Beispiel #1
0
        public static string Compute(HashType type, bool format, string data, string salt)
        {
            HashAlgorithm hash;
            string prefix;
            switch (type)
            {
                case HashType.MD5:
                    prefix = "MD5";
                    hash = new MD5Cng();
                    break;
                case HashType.SHA1:
                    prefix = "SHA1";
                    hash = new SHA1Cng();
                    break;
                case HashType.SHA256:
                    prefix = "SHA256";
                    hash = new SHA256Cng();
                    break;
                case HashType.SHA384:
                    prefix = "SHA384";
                    hash = new SHA384Cng();
                    break;
                case HashType.SHA512:
                    prefix = "SHA512";
                    hash = new SHA512Cng();
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }

            byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(salt+data);
            byte[] hashed = hash.ComputeHash(inputBytes);
            return format ? $"{{{prefix}:{GetHashHex(hashed)}}}" : GetHashHex(hashed);
        }
		public byte[] Hash(string plainText, byte[] salt)
		{
			var hashFunc = new SHA512Cng();
			var plainBytes = System.Text.Encoding.ASCII.GetBytes(plainText);
			var toHash = new byte[plainBytes.Length + salt.Length];
			plainBytes.CopyTo(toHash, 0);
			salt.CopyTo(toHash, plainBytes.Length);
			return hashFunc.ComputeHash(toHash);
		}
 internal static byte[] hash(string plaintext, byte[] salt)
 {
     SHA512Cng hashFunc = new SHA512Cng();
     byte[] plainBytes = System.Text.Encoding.ASCII.GetBytes(plaintext);
     byte[] toHash = new byte[plainBytes.Length + salt.Length];
     plainBytes.CopyTo(toHash, 0);
     salt.CopyTo(toHash, plainBytes.Length);
     return hashFunc.ComputeHash(toHash);
 }
Beispiel #4
0
        private async Task <string> HashToken(string token)
        {
            byte[] byteArray = Convert.FromBase64String(token);
            System.Security.Cryptography.SHA512Cng hasher = new System.Security.Cryptography.SHA512Cng();
            hasher.ComputeHash(byteArray);
            var output = Convert.ToBase64String(hasher.Hash);
            var size   = output.Length;

            return(output);
        }
 private static HashAlgorithm Sha512Hash( )
 {
     SHA512 hash;
     try
     {
         hash = new SHA512Cng( );
     }
     catch ( PlatformNotSupportedException )
     {
         hash = SHA512.Create( );
     }
     return hash;
 }
Beispiel #6
0
        public string ComputeHashUsingGivenSalt(string plain, string salt)
        {
            byte[] saltBytes = Convert.FromBase64String(salt);

            byte[] passwordBytes = System.Text.UnicodeEncoding.Unicode.GetBytes(plain);

            byte[] combinedBytes = new byte[passwordBytes.Length + saltBytes.Length];

            //Array src , int srcOffset, Array dst, int dstOffset, int count
            System.Buffer.BlockCopy(passwordBytes, 0, combinedBytes, 0, passwordBytes.Length);

            System.Buffer.BlockCopy(saltBytes, 0, combinedBytes, passwordBytes.Length, saltBytes.Length);

            HashAlgorithm hashAlgo = new SHA512Cng();

            byte[] hashBytes = hashAlgo.ComputeHash(combinedBytes);

            return Convert.ToBase64String(hashBytes);
        }
Beispiel #7
0
        /// <summary>
        /// Compute UDF from binary data and content type with specified precision.
        /// </summary>
        /// <param name="ContentType">MIME media type. See 
        /// http://www.iana.org/assignments/media-types/media-types.xhtml for list.</param>
        /// <param name="Data">Data to be fingerprinted.</param>
        /// <param name="Bits">Precision, must be a multiple of 25 bits.</param>
        /// <returns>The binary UDF fingerprint.</returns>
        public static byte[] From(string ContentType, byte[] Data, int Bits) {
            SHA512 SHA512 = new SHA512Cng();
            byte[] HashData = SHA512.ComputeHash(Data);

            SHA512.Initialize();

            var Tag = Encoding.UTF8.GetBytes(ContentType);
            byte[] Input = new byte[HashData.Length + Tag.Length + 1];

            int i = 0;
            foreach (var Byte in Tag) {
                Input[i++] = Byte;
                }
            Input[i++] = (byte)':';
            foreach (var Byte in HashData) {
                Input[i++] = Byte;
                }

            SHA512.Initialize();
            byte[] UDFData = SHA512.ComputeHash(Input);

            var TotalBits = Bits;
            var FullBytes = TotalBits / 8;
            var ExtraBits = TotalBits % 8;
            var TotalBytes = ExtraBits == 0 ? FullBytes : FullBytes + 1;

            byte[] Output = new byte[TotalBytes];
            Output[0] = (byte)UDFConstants.KeyIdentifierAlgSHA_2_512;
            for (var j = 0; j < FullBytes - 1; j++) {
                Output[j + 1] = UDFData[j];
                }

            if (ExtraBits > 0) {
                Output[TotalBytes - 1] = (byte)(UDFData[FullBytes - 1] << (8 - ExtraBits) & 0xff);
                }

            return Output;
            }
		protected override void SetUp ()
		{
			hash = new SHA512Cng ();
		}
Beispiel #9
0
        /// <summary>
        /// Genera un hash basado en SHA512 de largo definido a partir del contenido de una imagen.
        /// Lo devuelve como un array de bits.
        /// </summary>
        /// <param name="image">Imagen fuente</param>
        /// <param name="size">Largo en bits del hash</param>
        public static BitArray GenerateBitHashFromImage(Image image, int size)
        {
            //Hash de salida (Vector del tamaño deseado inicializado en 0)
            int sizeInBytes = (int) Math.Ceiling(size / 8f);
            byte[] hash = Enumerable.Repeat((byte) 0x00, sizeInBytes).ToArray();

            //Extrae el byteArray de la imagen
            byte[] imageBytes = ByteArrayFromImage(image);

            //Genera un hash de 192 bytes a base de aplicar el SHA512
            byte[] SHAComposedHash = new byte[192];
            using (var sha512 = new SHA512Cng())
            {
                //Hash de los bytes de la imagen
                Buffer.BlockCopy(sha512.ComputeHash(imageBytes), 0, SHAComposedHash, 0, 64);

                //Hash de los bytes invertidos de la imagen
                byte[] notImageBytes = new byte[imageBytes.Length];
                for (int i = 0; i < imageBytes.Length; i++)
                    notImageBytes[i] = (byte)~imageBytes[i];
                Buffer.BlockCopy(sha512.ComputeHash(notImageBytes), 0, SHAComposedHash, 64, 64);

                //Hash de los bytes del mismo hash
                byte[] currentHash = new byte[128];
                Buffer.BlockCopy(SHAComposedHash, 0, currentHash, 0, 128);
                Buffer.BlockCopy(sha512.ComputeHash(currentHash), 0, SHAComposedHash, 128, 64);
            }

            //Completa en unidades de bytes el largo faltante
            int iterations = 0;
            while (sizeInBytes > iterations * 192)
            {
                int toCopy = sizeInBytes - iterations * 192;
                Buffer.BlockCopy(SHAComposedHash, 0, hash, iterations++ * 192, Math.Min(192, toCopy));
            }

            //Recorta los bits sobrantes
            BitArray allBits = new BitArray(hash);
            BitArray finalBits = new BitArray(size);
            for (int i = 0; i < size; i++)
            {
                finalBits.Set(i, allBits.Get(i));
            }

            return finalBits;
        }
        /// <summary>
        /// Очистка конфиденциальных данных
        /// </summary>
        public void Clear()
        {
            // Указываем на деинициализацию
            IsInitialized = false;

            // Чистим массивы...
            CryforceUtilities.ClearArray(_key);
            CryforceUtilities.ClearArray(_IV);

            // Чистим криптографические сущности...
            if(_hash256 != null) _hash256.Clear();
            if(_hash512 != null) _hash512.Clear();
            if(_rijndael != null) _rijndael.Clear();

            // Инициализируем криптографические сущности...
            _hash256 = new SHA256Cng();
            _hash512 = new SHA512Cng();
            _rijndael = new RijndaelManaged();
        }
Beispiel #11
0
        /// <summary>
        /// Очистка конфиденциальных данных
        /// </summary>
        public void Clear()
        {
            // Указываем на деинициализацию
            IsInitialized = false;

            if(_ECDiffieHellmanCng != null)
            {
                _ECDiffieHellmanCng.Clear();
            }
            _ECDiffieHellmanCng = new ECDiffieHellmanCng();

            if(_ECDsaCng != null)
            {
                _ECDsaCng.Clear();
            }
            _ECDsaCng = new ECDsaCng();

            if(_hash512 != null)
            {
                _hash512.Clear();
            }
            _hash512 = new SHA512Cng();

            CryforceUtilities.ClearArray(Key512);
        }
Beispiel #12
0
		public override void SetUp ()
		{
			hash = new SHA512Cng ();
		}
 /// <summary>
 /// Computes the SHA512 Salted Hash of a password.
 /// </summary>
 /// <param name="password">The password to hash.</param>
 /// <param name="salt">The salt.</param>
 /// <returns>A SHA512 hash of the password.</returns>
 public byte[] ComputeHash_ShaSalt(string password, byte[] salt)
 {
     var sha = new SHA512Cng();
     var concatStr = _stringHandler.GetBytes(String.Concat(password, salt));
     return sha.ComputeHash(concatStr);
 }