Initialize() public final method

public final Initialize ( ) : void
return void
Example #1
0
 public static byte[] SHA256(string str)
 {
     SHA256 sha = new SHA256Managed();
     sha.Initialize();
     //return sha.ComputeHash(str.ToByteArray());
     return sha.ComputeHash(Encoding.UTF8.GetBytes(str));
 }
Example #2
0
 public static String hashString(String text)
 {
     SHA256 hasher = new SHA256Managed();
     hasher.Initialize();
     byte[] bytes = Encoding.Unicode.GetBytes(text);
     byte[] hash = hasher.ComputeHash(bytes);
     return BitConverter.ToString(hash).Replace("-", String.Empty);
 }
Example #3
0
 //Shy Ronnie
 private static string Sha256(string s)
 {
     var sh = new SHA256Managed();
     var request = Encoding.UTF8.GetBytes(s);
     sh.Initialize();
     var b4Bbuff = sh.ComputeHash(request, 0, request.Length);
     var b64 = Convert.ToBase64String(b4Bbuff);
     return b64.Substring(0, 43);
 }
        public static string CalculateFingerprintHash(string fingerprint)
        {
            SHA256Managed sh = new SHA256Managed();
            byte[] request = UTF8Encoding.UTF8.GetBytes(fingerprint);

            sh.Initialize();
            byte[] hash = sh.ComputeHash(request, 0, request.Length);

            return Convert.ToBase64String(hash);
        }
Example #5
0
        public byte[] GetHash(string password)
        {
            var passwordData = Encoding.Unicode.GetBytes(password);
            var toHashData = new byte[passwordData.Length + PasswordSalt.Length];
            passwordData.CopyTo(toHashData, 0);
            PasswordSalt.CopyTo(toHashData, passwordData.Length);

            var sha1 = new SHA256Managed();
            sha1.Initialize();
            return sha1.ComputeHash(toHashData);
        }
        public static String GenerateHash(String password)
        {
            //WARNING: This code does not reflect best practice.  It is a simplistic implementation designed to introduce the concept of hashing.
            password = "******" + password + "$#!%^";
            var pwdBytes = Encoding.UTF8.GetBytes(password);

            SHA256 hashAlg = new SHA256Managed();
            hashAlg.Initialize();
            var hashedBytes = hashAlg.ComputeHash(pwdBytes);
            var hash = Convert.ToBase64String(hashedBytes);
            return hash;
        }
 public User CreateNew(NewUser model)
 {
     using (var contextAccessor = new WriteContextAccessor())
     {
         SHA256Managed sha = new SHA256Managed();
         sha.Initialize();
         var user = new User
         {
             Name = model.UserName,
             PasswordHash = sha.ComputeHash(Encoding.UTF8.GetBytes(model.Password)).ToHexString()
         };
         contextAccessor.ReadWriteContext.Users.Add(user);
         contextAccessor.ReadWriteContext.SaveChanges();
         return user;
     }
 }
Example #8
0
        //===============================================================
        // Function: SHA256HashPassword
        //===============================================================
        private string SHA256HashPassword(string password)
        {
            SHA256Managed hashProvider;
            Byte[] passwordBytes;
            //Byte[] hashBytes;

            passwordBytes = System.Text.Encoding.Unicode.GetBytes(password);
            hashProvider = new SHA256Managed();
            hashProvider.Initialize();
            passwordBytes = hashProvider.ComputeHash(passwordBytes);
            string hashedPassword = Convert.ToBase64String(passwordBytes);

            return hashedPassword;
        }
Example #9
0
        public static byte[] GetBytes(byte[] password, byte[] salt, int iterations, int howManyBytes)
        {
            // round up

            uint cBlocks = (uint)((howManyBytes+ HASH_SIZE_IN_BYTES-1)/HASH_SIZE_IN_BYTES);

            // seed for the pseudo-random fcn: salt + block index
            byte[] saltAndIndex = new byte[salt.Length + 4];
            Array.Copy(salt, 0, saltAndIndex, 0, salt.Length);

            byte[] output = new byte[cBlocks*HASH_SIZE_IN_BYTES];
            int outputOffset = 0;

            SHA256Managed innerHash = new SHA256Managed();
            SHA256Managed outerHash = new SHA256Managed();

            // HMAC says the key must be hashed or padded with zeros
            // so it fits into a single block of the hash in use
            if (password.Length > BLOCK_SIZE_IN_BYTES) {
                password = innerHash.ComputeHash(password);
            }
            byte[] key = new byte[BLOCK_SIZE_IN_BYTES];
            Array.Copy(password, 0, key, 0, password.Length);

            byte[] InnerKey = new byte[BLOCK_SIZE_IN_BYTES];
            byte[] OuterKey = new byte[BLOCK_SIZE_IN_BYTES];
            for (int i = 0; i < BLOCK_SIZE_IN_BYTES; ++i) {
                InnerKey[i] = (byte)(key[i] ^ IPAD);
                OuterKey[i] = (byte)(key[i] ^ OPAD);
            }

            // for each block of desired output
            for (int iBlock = 0; iBlock < cBlocks; ++iBlock) {
                // seed HMAC with salt & block index
                _incrementBigEndianIndex(saltAndIndex, salt.Length);
                byte[] U = saltAndIndex;

                for (int i = 0; i < iterations; ++i) {
                    // simple implementation of HMAC-SHA-256
                    innerHash.Initialize();
                    innerHash.TransformBlock(InnerKey, 0,
                        BLOCK_SIZE_IN_BYTES, InnerKey, 0);
                    innerHash.TransformFinalBlock(U, 0, U.Length);

                    byte[] temp = innerHash.Hash;

                    outerHash.Initialize();
                    outerHash.TransformBlock(OuterKey, 0,
                        BLOCK_SIZE_IN_BYTES, OuterKey, 0);
                    outerHash.TransformFinalBlock(temp, 0, temp.Length);

                    U = outerHash.Hash; // U = result of HMAC

                    // xor result into output buffer
                    _xorByteArray(U, 0, HASH_SIZE_IN_BYTES,
                        output, outputOffset);
                }
                outputOffset += HASH_SIZE_IN_BYTES;
            }
            byte[] result = new byte[howManyBytes];
            Array.Copy(output, 0, result, 0, howManyBytes);
            return result;
        }
Example #10
0
        public static string GetJWT(string clientEmail, RSACryptoServiceProvider privateKey, DateTime now) {
            var payload = new {
                scope = scope,
                iss = clientEmail,
                aud = "https://accounts.google.com/o/oauth2/token",
                exp = (int)(now - zeroDate + TimeSpan.FromHours(1)).TotalSeconds,
                iat = (int)(now - zeroDate).TotalSeconds,
                //sub = "*****@*****.**",
            };

            string serializedPayload = JsonConvert.SerializeObject(payload);

            using (var hashAlg = new SHA256Managed()) {
                hashAlg.Initialize();
                var headerAndPayload = UrlBase64Encode(serializedHeader) + "." + UrlBase64Encode(serializedPayload);
                var headerPayloadBytes = Encoding.ASCII.GetBytes(headerAndPayload);
                var signature = UrlBase64Encode(privateKey.SignData(headerPayloadBytes, hashAlg));
                return headerAndPayload + "." + signature;
            }

        }
Example #11
0
 /// <summary>
 /// Encrypts a given string (password) using the SHA1 cryptography algorithm
 /// </summary>
 /// <param name="password">string (passowrd) to encrypt</param>
 /// <returns>Encrypted hash for the supplied string (password)</returns>
 public string HashPassword(string password)
 {
     Byte[] passwordBytes = Encoding.Unicode.GetBytes(password);
     SHA256Managed hashProvider = new SHA256Managed();
     hashProvider.Initialize();
     passwordBytes = hashProvider.ComputeHash(passwordBytes);
     hashProvider.Clear();
     return Convert.ToBase64String(passwordBytes);
 }
Example #12
0
 //Create a string from the input
 private static string Hash(string url)
 {
     byte[] result;
     SHA256 shaM = new SHA256Managed();
     byte[] ms = new byte[url.Length];
     for (int i = 0; i < url.Length; i++)
     {
         byte b = Convert.ToByte(url[i]);
         ms[i] = (b);
     }
     shaM.Initialize();
     result = shaM.ComputeHash(ms, 0, ms.Length);
     return BitConverter.ToString(result);
 }