Example #1
0
    private string EncryptPasswrod(string password)
    {
        System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();
        string hashed = System.Convert.ToBase64String(sha.ComputeHash(System.Text.UnicodeEncoding.Unicode.GetBytes(password)));

        return(hashed.Length > 49 ? hashed.Substring(0, 49) : hashed);
    }
Example #2
0
    public static void CalculaterHash()
    {
        string assetPath    = AssetDatabase.GetAssetPath(Selection.activeObject);
        string absulotePath = Application.dataPath + assetPath.Substring(6);

        FileStream fs = new FileStream(absulotePath, FileMode.Open, FileAccess.Read);

        //md5
        System.Security.Cryptography.MD5 md5calculator = System.Security.Cryptography.MD5.Create();
        byte[]        buffer1 = md5calculator.ComputeHash(fs);
        StringBuilder sb1     = new StringBuilder();

        for (int i = 0; i < buffer1.Length; ++i)
        {
            sb1.Append(buffer1[i].ToString("x2"));
        }

        Debug.LogWarningFormat("MD5:{0}", sb1.ToString());

        System.Security.Cryptography.SHA1 sha1calculator = System.Security.Cryptography.SHA1.Create();
        byte[]        buffer2 = sha1calculator.ComputeHash(fs);
        StringBuilder sb2     = new StringBuilder();

        for (int i = 0; i < buffer2.Length; ++i)
        {
            sb2.Append(buffer2[i].ToString("x2"));
        }

        Debug.LogWarningFormat("SHA1:{0}", sb2.ToString());

        fs.Close();
    }
Example #3
0
        /// <summary>
        /// 将字符串进行SHA1加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string SHA1(string str)
        {
            System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
            string encoded = BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", "");

            return(encoded);
        }
Example #4
0
        public static Hash Compute(byte[] contentData)
        {
            System.Security.Cryptography.SHA1 hashAlgorithm = System.Security.Cryptography.SHA1.Create();
            byte[] hashData = hashAlgorithm.ComputeHash(contentData);

            return(new Hash(hashData));
        }
Example #5
0
    //----------------------------------------------------------------------------

    /*!
     *          @brief	セキュリティ用チェックサムの構築
     */
    //----------------------------------------------------------------------------
    static public string CreatePacketCheckSum(string strPacketJson,
                                              SERVER_API packetAPI = SERVER_API.SERVER_API_NONE,
                                              string uuid          = null)
    {
        {
            //sha1
            System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();

            string checkSumUuid = (uuid != null) ? uuid : LocalSaveManager.Instance.LoadFuncUUID();
            //ユーザー作成時は正常終了後にuuidセットしてるので送ったjsonからuuid取り出す
            if (packetAPI == SERVER_API.SERVER_API_USER_CREATE)
            {
                //JsonData型にパース
                JsonData sendMessageJsonData = JsonMapper.ToObject(strPacketJson);
                //パース後のJsonData型にuuidがあるか?
                if (true == (sendMessageJsonData as IDictionary).Contains("uuid"))
                {
                    //jsonのuuidを設定
                    checkSumUuid = (string)sendMessageJsonData["uuid"];
                }
            }

            var sha1Bs = sha1.ComputeHash(new UTF8Encoding().GetBytes(strPacketJson + checkSumUuid));
            strPacketJson = BitConverter.ToString(sha1Bs).ToLower().Replace("-", "");
        }

        return("");
    }
Example #6
0
	}//ComputeCRC32


	/// <summary>
	/// 获取文件的SHA1
	/// </summary>
	/// <param name="fileName"></param>
	/// <returns></returns>
	public static String GetSHA1(String fileName)
	{
		String hashSHA1 = String.Empty;
		//检查文件是否存在,如果文件存在则进行计算,否则返回空值
		if (File.Exists(fileName))
		{
			using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
			{
				//计算文件的SHA1值
				System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create();
				Byte[] buffer = calculator.ComputeHash(fileStream);
				calculator.Clear();
				//将字节数组转换成十六进制的字符串形式
				StringBuilder stringBuilder = new StringBuilder();
				for (int bufferIdx = 0; bufferIdx < buffer.Length; bufferIdx++)
				{
					stringBuilder.Append(buffer[bufferIdx].ToString("x2"));
				}
				hashSHA1 = stringBuilder.ToString();

			}//关闭文件流
		}
		else
		{
			Console.Error.WriteLine("{0}文件找不到!", fileName);
		}
		return hashSHA1;
	}//end GetSHA1
Example #7
0
 /// <summary>
 /// //Returns a SHA1 hash of the value of msDS-UpdateScript of Partitions container.
 /// </summary>
 public static byte[] PrepareScriptHashBody(string updateScript)
 {
     byte[] unicodeBytes = System.Text.Encoding.Unicode.GetBytes(updateScript);
     System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1CryptoServiceProvider.Create();
     byte[] hash1 = sha.ComputeHash(unicodeBytes);
     return(hash1);
 }
    static private string Sha1BattleSetting(string values)
    {
        System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
        var sha1Bs = sha1.ComputeHash(new UTF8Encoding().GetBytes(values));

        return(BitConverter.ToString(sha1Bs).ToLower().Replace("-", ""));
    }
Example #9
0
 private static byte[] SHA1(params byte[][] data)
 {
     using (System.Security.Cryptography.SHA1 alg = CryptoNS.SHA1.Create())
     {
         return(alg.ComputeHash(Combine(data)));
     }
 }
 string Hash(SHA1 sha, int number)
 {
     byte[] hashArray = sha.ComputeHash(Encoding.ASCII.GetBytes(number.ToString()));
     string newHash = "";
     for (int hashPos = 0; hashPos < hashArray.Length; hashPos++)
         newHash += hashArray[hashPos].ToString("x2");
     return newHash;
 }
Example #11
0
        public static string doHash(string passwordPlain, string salt)
        {
            System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();
            byte[] preHash  = System.Text.Encoding.UTF32.GetBytes(passwordPlain + salt);
            byte[] hash     = sha.ComputeHash(preHash);
            string password = System.Convert.ToBase64String(hash, 0, hash.Length);

            return(password);
        }
        public static byte[] GenerateOtp(string appKey, string userKey, string challenge, OtpAlgorithmEnum AuthAlgorithm)
        {
            byte[] uKey  = Encoding.UTF8.GetBytes(userKey);
            byte[] aKey  = Encoding.UTF8.GetBytes(appKey);
            byte[] chall = Encoding.UTF8.GetBytes(challenge);

            byte[] inBuf = new byte[uKey.Length + aKey.Length + chall.Length];

            int j = 0;

            for (int i = 0; i < uKey.Length; i++)
            {
                inBuf[j++] = uKey[i];
            }

            for (int i = 0; i < aKey.Length; i++)
            {
                inBuf[j++] = aKey[i];
            }

            for (int i = 0; i < chall.Length; i++)
            {
                inBuf[j++] = chall[i];
            }

            byte[] hashedvalue = null;
            switch (AuthAlgorithm)
            {
            case OtpAlgorithmEnum.Md5:
                System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
                hashedvalue = md5.ComputeHash(inBuf);
                break;

            case OtpAlgorithmEnum.Sha1:
                System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
                hashedvalue = sha1.ComputeHash(inBuf);
                break;

            case OtpAlgorithmEnum.Des:
                int lenAdj = inBuf.Length % 8;
                if (lenAdj != 0)
                {
                    lenAdj = 8 - lenAdj;
                }
                //int byteLen =
                byte[] temp = new byte[inBuf.Length + lenAdj];
                Array.Copy(inBuf, temp, inBuf.Length);
                for (int i = 0; i < lenAdj; i++)
                {
                    temp[inBuf.Length + i] = 0x00;
                }

                hashedvalue = Encrypt3DES(new MemoryStream(temp), uKey, new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }).ToArray();
                break;
            }
            return(hashedvalue);
        }
Example #13
0
        public override string Hash(BufferedStream stream)
        {
            byte[] checksum = null;
            System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();
            checksum = sha.ComputeHash(stream);
            string hashResult = BitConverter.ToString(checksum).Replace("-", String.Empty);

            return(hashResult);
        }
 /// <summary>
 /// CheckSum160 method implementation
 /// </summary>
 public static byte[] CheckSum160(string value)
 {
     byte[] hash = null;
     using (System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1Cng.Create())
     {
         hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(value));
     }
     return(hash);
 }
 /// <summary>
 /// CheckSum160 method implementation
 /// </summary>
 public static byte[] CheckSum160(byte[] value)
 {
     byte[] hash = null;
     using (System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1Cng.Create())
     {
         hash = sha1.ComputeHash(value);
     }
     return(hash);
 }
Example #16
0
        public static Hash Compute(Stream contentStream)
        {
            System.Security.Cryptography.SHA1 hashAlgorithm = System.Security.Cryptography.SHA1.Create();
            long position = contentStream.Position;

            byte[] hashData = hashAlgorithm.ComputeHash(contentStream);
            contentStream.Position = position;

            return(new Hash(hashData));
        }
Example #17
0
        /// <summary>
        /// take any string and encrypt it using SHA1 then
        /// return the encrypted data
        /// </summary>
        /// <param name="data">input text you will enterd to encrypt it</param>
        /// <returns>return the encrypted text as hexadecimal string</returns>
        public static string GetSHA1HashData(string data)
        {
            System.Security.Cryptography.SHA1 hash    = System.Security.Cryptography.SHA1.Create();
            System.Text.ASCIIEncoding         encoder = new System.Text.ASCIIEncoding();
            byte[] combined = encoder.GetBytes(data);
            hash.ComputeHash(combined);
            string rethash = Convert.ToBase64String(hash.Hash);

            return(rethash);
        }
Example #18
0
    /// <summary>
    /// This function is used for encryption.
    /// </summary>
    /// <param name="strCode">The code.</param>
    /// <returns>string</returns>
    public static string Sha1Hash(string strCode)
    {
        string rehash = "";

        System.Security.Cryptography.SHA1 hash    = System.Security.Cryptography.SHA1.Create();
        System.Text.ASCIIEncoding         encoder = new System.Text.ASCIIEncoding();
        byte[] combined = encoder.GetBytes(strCode);
        hash.ComputeHash(combined);
        rehash = Convert.ToBase64String(hash.Hash);
        return(rehash);
    }
Example #19
0
        /// <summary>返回sha1算法小写字符串</summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string SHA1(string Text)
        {
            StringBuilder _result = new StringBuilder();

            System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
            byte[] sha1Arr = sha1.ComputeHash(Encoding.UTF8.GetBytes(Text));
            foreach (var b in sha1Arr)
            {
                _result.AppendFormat("{0:x2}", b);
            }
            return(_result.ToString().ToLower());
        }
Example #20
0
        public static string GetSwcSH1(string Password)
        {
            System.Security.Cryptography.SHA1 algorithm = SHA1.Create();
            byte[] data = algorithm.ComputeHash(Encoding.UTF8.GetBytes(Password));
            string sh1  = "";

            for (int i = 0; i < data.Length; i++)
            {
                sh1 += data[i].ToString("x2").ToUpperInvariant();
            }
            return(sh1);
        }
Example #21
0
    string GetSha1(string input)
    {
        System.Security.Cryptography.SHA1 sha1Hash = System.Security.Cryptography.SHA1.Create();
        StringBuilder sBuilder = new StringBuilder();

        byte[] data = sha1Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }
        return(sBuilder.ToString());
    }
Example #22
0
        /// <summary>
        /// Mixes password and salt and hashes them
        /// </summary>
        /// <param name="password">The password.</param>
        /// <param name="salt">The salt.</param>
        /// <returns>Hashed Password string</returns>
        public static string CreatePasswordHash(string password, string salt)
        {
            string saltAndPwd = "mixing" + password + "with some" + salt;

            System.Security.Cryptography.SHA1 hash    = System.Security.Cryptography.SHA1.Create();
            System.Text.UTF8Encoding          encoder = new System.Text.UTF8Encoding();
            byte[] combined = encoder.GetBytes(saltAndPwd);
            hash.ComputeHash(combined);
            string hashCode = Convert.ToBase64String(hash.Hash);

            return(hashCode);
        }
Example #23
0
        private string GetSHA1HashData(string data)
        {
            System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
            byte[]        hashData    = sha1.ComputeHash(ASCIIEncoding.Default.GetBytes(data));
            StringBuilder returnValue = new StringBuilder();

            for (int i = 0; i < hashData.Length; i++)
            {
                returnValue.Append(hashData[i].ToString());
            }
            return(returnValue.ToString());
        }
Example #24
0
        public static string SHA1(string orgStr)
        {
            string strResult = "";

            System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();
            byte[] bytResult = sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(orgStr));
            for (int i = 0; i < bytResult.Length; i++)
            {
                strResult = strResult + bytResult[i].ToString("x2");
            }
            return(strResult);
        }
Example #25
0
        public override string StringHashAlgorithm(string value)
        {
            System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
            byte[]        bytes = sha1.ComputeHash(System.Text.ASCIIEncoding.UTF8.GetBytes(value));
            StringBuilder sb    = new StringBuilder();

            foreach (var b in bytes)
            {
                sb.Append(b.ToString("x2"));
            }
            return(sb.ToString());
        }
Example #26
0
        /// <summary>
        /// Returns a SHA1 hash of the value formed by appending the GUID {0916C8E3-3431-4586-AF77-44BD3B16F961}
        /// to the value of msDS-UpdateScript of Partitions container.
        /// </summary>
        public static byte[] PrepareScriptHashSignature(string updateScript)
        {
            byte[] unicodeBytes = System.Text.Encoding.Unicode.GetBytes(updateScript);
            System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1CryptoServiceProvider.Create();
            Guid        g           = new Guid("0916C8E3-3431-4586-AF77-44BD3B16F961");
            List <byte> rawByteList = new List <byte>();

            rawByteList.AddRange(unicodeBytes);
            rawByteList.AddRange(g.ToByteArray());

            byte[] hash1 = sha.ComputeHash(rawByteList.ToArray());
            return(hash1);
        }
Example #27
0
    private static string GetHashString(string unhashed)
    {
        byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(unhashed);
        System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();
        byte[] hash = sha.ComputeHash(inputBytes);
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        for (int i = 0; i < hash.Length; i++)
        {
            sb.Append(hash[i].ToString("x2"));
        }

        return(sb.ToString());
    }
Example #28
0
        private static Sha1 HashImpl(crypt.SHA1 alg, ReadOnlySpan <byte> span)
        {
            Debug.Assert(alg != null);

            // Do NOT short-circuit here; rely on call-sites to do so
#if !NETSTANDARD2_0
            Span <byte> hash = stackalloc byte[Sha1.ByteLength];
            alg.TryComputeHash(span, hash, out _);
#else
            var hash = alg.ComputeHash(span.ToArray());
#endif
            var sha = new Sha1(hash);
            return(sha);
        }
Example #29
0
 /// <summary>
 /// SHA1加密(40位字符)
 /// </summary>
 /// <param name="strSource">源字符串</param>
 /// <param name="encoding">编码</param>
 /// <returns>加密后的字符串</returns>
 public static string GetSHA1(string strSource, System.Text.Encoding encoding = null)
 {
     StringBuilder strResult = new StringBuilder();
     if (encoding == null)
     {
         encoding = Encoding.UTF8;
     }
     System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();
     byte[] bytResult = sha.ComputeHash(encoding.GetBytes(strSource));
     for (int i = 0, len = bytResult.Length; i < len; i++)
     {
         strResult.Append(bytResult[i].ToString("X2"));
     }
     return strResult.ToString();
 }
Example #30
0
        // sha1 below
        public static string GetSHA1Hash(string str)
        {
            System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
            byte[] bytes   = Encoding.Default.GetBytes(str);
            byte[] encoded = sha1.ComputeHash(bytes);

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < encoded.Length; i++)
            {
                sb.Append(encoded[i].ToString("x2"));
            }

            return(sb.ToString());
        }
Example #31
0
        public string GetSha1(string str)
        {
            System.Security.Cryptography.SHA1 sha1 = SHA1Managed.Create();
            ASCIIEncoding encoding = new ASCIIEncoding();

            byte[]        stream = null;
            StringBuilder sb     = new StringBuilder();

            stream = sha1.ComputeHash(encoding.GetBytes(str));
            for (int i = 0; i < stream.Length; i++)
            {
                sb.AppendFormat("{0:x2}", stream[i]);
            }
            return(sb.ToString());
        }
Example #32
0
        // get sha1 hash as string from string
        private static string GetSha1Hash(SHA1 sha1Hash, string input)
        {
            // Convert the input string to a byte array and compute the hash.
            byte[] data = sha1Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }
Example #33
0
 //compares SHA1 of i with global hash
 public bool ShaTest(int i, SHA1 sha)
 {
     byte[] hashArray = sha.ComputeHash(Encoding.ASCII.GetBytes(i.ToString()));
     string newHash = "";
     for (int hashPos = 0; hashPos < hashArray.Length; hashPos++)
         newHash += hashArray[hashPos].ToString("x2");
     return newHash == hash;
 }
Example #34
0
	public void FIPS186_a (string testName, SHA1 hash, byte[] input, byte[] result) 
	{
		byte[] output = hash.ComputeHash (input); 
		AssertEquals (testName + ".a.1", result, output);
		AssertEquals (testName + ".a.2", result, hash.Hash);
		// required or next operation will still return old hash
		hash.Initialize ();
	}
	public void FIPS186_c (string testName, SHA1 hash, byte[] input, byte[] result) 
	{
		MemoryStream ms = new MemoryStream (input);
		byte[] output = hash.ComputeHash (ms); 
		Assert.AreEqual (result, output, testName + ".c.1");
		Assert.AreEqual (result, hash.Hash, testName + ".c.2");
		// required or next operation will still return old hash
		hash.Initialize ();
	}
	public void FIPS186_b (string testName, SHA1 hash, byte[] input, byte[] result) 
	{
		byte[] output = hash.ComputeHash (input, 0, input.Length); 
		Assert.AreEqual (result, output, testName + ".b.1");
		Assert.AreEqual (result, hash.Hash, testName + ".b.2");
		// required or next operation will still return old hash
		hash.Initialize ();
	}
Example #37
0
        private string GetSHA1Hash(SHA1 sha1Hash, string source)
        {
            byte[] data = sha1Hash.ComputeHash(Encoding.UTF8.GetBytes(source));

            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return sBuilder.ToString();
        }