Ejemplo n.º 1
0
 public static byte[] GenerateHash(byte[] inputBytes)
 {
     using (System.Security.Cryptography.SHA256 sha256 = System.Security.Cryptography.SHA256Managed.Create())
     {
         return(sha256.ComputeHash(inputBytes));
     }
 }
Ejemplo n.º 2
0
        public static string GetSHA512HashAsHexString(byte[] data)
        {
            System.Security.Cryptography.SHA256 sha512Implementation = System.Security.Cryptography.SHA256.Create();
            var hashData = sha512Implementation.ComputeHash(data);

            return(GeneralConverters.ByteArrayToHexString(hashData));
        }
 public static string GetSHA256(this byte[] input, int offset, int count)
 {
     using (System.Security.Cryptography.SHA256 sha256 = System.Security.Cryptography.SHA256.Create())
     {
         return(Convert.ToHexString(sha256.ComputeHash(input, offset, count)).ToLower());
     }
 }
Ejemplo n.º 4
0
        //public static string GetAddressFromPublicKey(byte[] publickey)
        //{
        //    byte[] scriptHash = GetScriptHashFromPublicKey(publickey);
        //    return GetAddressFromScriptHash(scriptHash);
        //}
        //public static byte[] GetPublicKeyHash(byte[] publickey)
        //{
        //    var hash1 = sha256.ComputeHash(publickey);
        //    var hash2 = ripemd160.ComputeHash(hash1);
        //    return hash2;
        //}
        public static Hash160 GetPublicKeyHashFromAddress(string address)
        {
            var alldata = Base58.Decode(address);

            if (alldata.Length != 25)
            {
                throw new Exception("error length.");
            }
            var data = alldata.Take(alldata.Length - 4).ToArray();

            if (data[0] != 0x17)
            {
                throw new Exception("not a address");
            }
            System.Security.Cryptography.SHA256 sha256 = getSha256();
            var hash = sha256.ComputeHash(data);

            hash = sha256.ComputeHash(hash);
            var hashbts     = hash.Take(4).ToArray();
            var datahashbts = alldata.Skip(alldata.Length - 4).ToArray();

            if (hashbts.SequenceEqual(datahashbts) == false)
            {
                throw new Exception("not match hash");
            }
            var pkhash = data.Skip(1).ToArray();

            return(new Hash160(pkhash));
        }
Ejemplo n.º 5
0
 public static string CalculateHash(string arg)
 {
     System.Security.Cryptography.SHA256 SHA256Calculator = System.Security.Cryptography.SHA256.Create();
     byte[] hash = new byte[32];
     hash = SHA256Calculator.ComputeHash(Encoding.UTF8.GetBytes(arg));
     return(BitConverter.ToString(hash).Replace("-", string.Empty)); //converts 32 bytes of hash to 64 HEX letters
 }
        public static string GetSha(string txt, string salt = "Tekin")
        {
            StringBuilder sb = new StringBuilder();

            //创建一个计算SHA256值的对象
            using (System.Security.Cryptography.SHA256 mysha = System.Security.Cryptography.SHA256.Create())
            {
                //把字符串转换为byte数组
                byte[] bytes = System.Text.Encoding.UTF8.GetBytes(txt + salt);
                //将字符数组转换为字符串
                //string str = Encoding.UTF8.GetString(bytes);

                //调用该对象的方法进行md5计算
                byte[] shabyte = mysha.ComputeHash(bytes);

                //把结果以字符串的显示返回
                for (int i = 0; i < shabyte.Length; i++)
                {
                    //这里的 x2    x表示以小写16进制显示字符, 2表示位宽位2位不足的前面补0
                    sb.Append(shabyte[i].ToString("x2"));
                }
                //返回字符串
                return(sb.ToString());
            }
        }
Ejemplo n.º 7
0
        private string GetSHA256(string str)
        {
            StringBuilder builder = new StringBuilder();

            if (!string.IsNullOrEmpty(str.Trim()))
            {
                using (System.Security.Cryptography.SHA256 mySHA256 = System.Security.Cryptography.SHA256.Create())
                {
                    try
                    {
                        // ComputeHash - returns byte array
                        byte[] bytes = mySHA256.ComputeHash(Encoding.UTF8.GetBytes(str));

                        // Convert byte array to a string
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            builder.Append(bytes[i].ToString("x2"));
                        }
                    }
                    catch (UnauthorizedAccessException err)
                    {
                        throw err;
                    }
                }
            }

            return(builder.ToString());
        }
Ejemplo n.º 8
0
 public static byte[] ComputeHash(byte[] data)
 {
     using (System.Security.Cryptography.SHA256 sha256Hash = System.Security.Cryptography.SHA256.Create())
     {
         return(sha256Hash.ComputeHash(data));
     }
 }
Ejemplo n.º 9
0
 public static byte[] ComputeSHA256HashByte(this string input)
 {
     using (System.Security.Cryptography.SHA256 sha = System.Security.Cryptography.SHA256.Create())
     {
         return(sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(input)));
     }
 }
Ejemplo n.º 10
0
 public string Generate(string file)
 {
     using (System.Security.Cryptography.SHA256 sha256 = System.Security.Cryptography.SHA256.Create())
     {
         return(BitConverter.ToString(sha256.ComputeHash(Encoding.UTF8.GetBytes(file))));
     }
 }
Ejemplo n.º 11
0
        public static byte[] GetPrivateKeyFromWIF(string wif)
        {
            if (wif == null)
            {
                throw new ArgumentNullException();
            }
            byte[] data = Base58.Decode(wif);
            //检查标志位
            if (data.Length != 38 || data[0] != 0x80 || data[33] != 0x01)
            {
                throw new Exception("wif length or tag is error");
            }
            //取出检验字节
            var sum = data.Skip(data.Length - 4);

            byte[] realdata = data.Take(data.Length - 4).ToArray();

            //验证,对前34字节进行进行两次hash取前4个字节
            System.Security.Cryptography.SHA256 sha256 = getSha256();
            byte[] checksum = sha256.ComputeHash(realdata);
            checksum = sha256.ComputeHash(checksum);
            var sumcalc = checksum.Take(4);

            if (sum.SequenceEqual(sumcalc) == false)
            {
                throw new Exception("the sum is not match.");
            }

            byte[] privateKey = new byte[32];
            Buffer.BlockCopy(data, 1, privateKey, 0, privateKey.Length);
            Array.Clear(data, 0, data.Length);
            return(privateKey);
        }
Ejemplo n.º 12
0
        public static byte[] PublicKeyHash(byte[] bytes)
        {
            var hash  = SHA256.Create();
            var riper = new RIPEMD160();

            bytes = hash.ComputeHash(bytes, 0, bytes.Length);
            return(riper.ComputeHash(bytes, 0, bytes.Length));
        }
Ejemplo n.º 13
0
 private static string GetSha256Hash(string data)
 {
     using (System.Security.Cryptography.SHA256 sha256Hash = System.Security.Cryptography.SHA256.Create())
     {
         byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(data));
         return(GetHex(bytes));
     }
 }
Ejemplo n.º 14
0
 public void Create()
 {
     if (this.mSHA256 != null)
     {
         return;
     }
     this.mSHA256 = System.Security.Cryptography.SHA256.Create();
 }
Ejemplo n.º 15
0
        public static string GetHash(List <byte> inputBuffer)
        {
            System.Security.Cryptography.SHA256 shaHasher = System.Security.Cryptography.SHA256.Create();

            byte[] hash = shaHasher.ComputeHash(inputBuffer.ToArray());

            return(Convert.ToBase64String(hash));
        }
Ejemplo n.º 16
0
        public string Criptografar(string mensagem)
        {
            System.Security.Cryptography.SHA256 sha256 = System.Security.Cryptography.SHA256.Create();
            byte[] mensagemBytes     = Encoding.UTF8.GetBytes(mensagem);
            byte[] criptografiaBytes = sha256.ComputeHash(mensagemBytes);
            string criptografia      = Convert.ToBase64String(criptografiaBytes);

            return(criptografia);
        }
Ejemplo n.º 17
0
 public void Clear()
 {
     if (this.mSHA256 == null)
     {
         return;
     }
     this.mSHA256.Clear();
     this.mSHA256 = (System.Security.Cryptography.SHA256)null;
 }
Ejemplo n.º 18
0
 public UserController(IEmailSender emailSender, ReleaseasyContext context, ILogger <UserController> logger, UserManager <User> userManager, SignInManager <User> signInManager)
 {
     this.emailSender   = emailSender;
     this.context       = context;
     hashingAlgorithm   = System.Security.Cryptography.SHA256.Create();
     this.logger        = logger;
     this.userManager   = userManager;
     this.signInManager = signInManager;
 }
Ejemplo n.º 19
0
		public WebCrawler(WebBrowser browser, string dir)
		{
			_IsActive = true;
			_Browser = browser;
			//_Browser.ScriptErrorsSuppressed = true;
			_Browser.DocumentCompleted += DocumentCompleted;
			_ImageDirectory = dir;
			_Sha256 = System.Security.Cryptography.SHA256Managed.Create();
		}
Ejemplo n.º 20
0
        public static Hash160 GetScriptHashFromScript(byte[] script)
        {
            System.Security.Cryptography.SHA256 sha256 = getSha256();
            var scripthash             = sha256.ComputeHash(script);
            RIPEMD160Managed ripemd160 = getRipemd160();

            scripthash = ripemd160.ComputeHash(scripthash);
            return(scripthash);
        }
Ejemplo n.º 21
0
 public string Hash()
 {
     if (!IsValid())
     {
         return("");
     }
     using var sha = SHA256.Create();
     using var f   = Char.OpenRead();
     return(Convert.ToBase64String(sha.ComputeHash(f)));
 }
Ejemplo n.º 22
0
 public string HashMetadata()
 {
     if (!IsValid())
     {
         return("");
     }
     using var sha = SHA256.Create();
     using var s   = Metadata.OpenRead();
     return(Convert.ToBase64String(sha.ComputeHash(s)));
 }
Ejemplo n.º 23
0
        /// <summary>
        /// Multi Convert 3 Steps
        /// </summary>
        /// <param name="value">Raw Value</param>
        /// <returns>SHA256 String</returns>
        public static string ConversionCheckDevice(int value)
        {
            /*
             *  Function Multi Convert 3 Steps
             *  Recommended for Serial Number
             *  Flow : RAW-Values --> Binary --> Byte --> SHA256 --> Validation DB
             */

            string ResultBinary = "";

            byte[] ResultBytes      = null;
            string ResultByteString = "";
            string ResultEncrypt256 = "";

            try
            {
                // SerialNumber (Decimal) --> Binary
                while (value > 1)
                {
                    int remainder = value % 2;
                    ResultBinary = Convert.ToString(remainder) + ResultBinary;
                    value       /= 2;
                }
                ResultBinary = Convert.ToString(value) + ResultBinary;

                // Binary --> Byte
                int numOfBytes = ResultBinary.Length / 8;
                ResultBytes = new byte[numOfBytes];
                for (int i = 0; i < numOfBytes; ++i)
                {
                    ResultBytes[i] = Convert.ToByte(ResultBinary.Substring(8 * i, 8), 2);
                }

                // Byte --> String
                int ResultBytesLength = ResultBytes.Length;
                for (int i = 0; i < ResultBytesLength; i++)
                {
                    ResultByteString += ResultBytes[i].ToString();
                }

                // String --> SHA256 --> String
                using (System.Security.Cryptography.SHA256 encrypt = System.Security.Cryptography.SHA256.Create())
                {
                    byte[]        bytes      = encrypt.ComputeHash(Encoding.UTF8.GetBytes(ResultByteString));
                    StringBuilder strBuilder = new StringBuilder();
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        strBuilder.Append(bytes[i].ToString("x2"));
                    }
                    ResultEncrypt256 = strBuilder.ToString();
                }
            }
            catch { }
            return(ResultEncrypt256);
        }
Ejemplo n.º 24
0
        public static byte[] Sign(byte[] message, byte[] prikey)
        {
            var Secp256r1_G = Helper.HexString2Bytes("04" + "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296" + "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");

            var PublicKey = ThinNeo.Cryptography.ECC.ECCurve.Secp256r1.G * prikey;
            var pubkey    = PublicKey.EncodePoint(false).Skip(1).ToArray();

            var ecdsa = new ThinNeo.Cryptography.ECC.ECDsa(prikey, ThinNeo.Cryptography.ECC.ECCurve.Secp256r1);

            System.Security.Cryptography.SHA256 sha256 = getSha256();
            var hash   = sha256.ComputeHash(message);
            var result = ecdsa.GenerateSignature(hash);
            var data1  = result[0].ToByteArray();

            if (data1.Length > 32)
            {
                data1 = data1.Take(32).ToArray();
            }
            var data2 = result[1].ToByteArray();

            if (data2.Length > 32)
            {
                data2 = data2.Take(32).ToArray();
            }

            data1 = data1.Reverse().ToArray();
            data2 = data2.Reverse().ToArray();

            byte[] newdata = new byte[64];
            Array.Copy(data1, 0, newdata, 32 - data1.Length, data1.Length);
            Array.Copy(data2, 0, newdata, 64 - data2.Length, data2.Length);

            return(newdata);// data1.Concat(data2).ToArray();
            //#if NET461
            //const int ECDSA_PRIVATE_P256_MAGIC = 0x32534345;
            //byte[] first = { 0x45, 0x43, 0x53, 0x32, 0x20, 0x00, 0x00, 0x00 };
            //prikey = first.Concat(pubkey).Concat(prikey).ToArray();
            //using (System.Security.Cryptography.CngKey key = System.Security.Cryptography.CngKey.Import(prikey, System.Security.Cryptography.CngKeyBlobFormat.EccPrivateBlob))
            //using (System.Security.Cryptography.ECDsaCng ecdsa = new System.Security.Cryptography.ECDsaCng(key))

            //using (var ecdsa = System.Security.Cryptography.ECDsa.Create(new System.Security.Cryptography.ECParameters
            //{
            //    Curve = System.Security.Cryptography.ECCurve.NamedCurves.nistP256,
            //    D = prikey,
            //    Q = new System.Security.Cryptography.ECPoint
            //    {
            //        X = pubkey.Take(32).ToArray(),
            //        Y = pubkey.Skip(32).ToArray()
            //    }
            //}))
            //{
            //    var hash = sha256.ComputeHash(message);
            //    return ecdsa.SignHash(hash);
            //}
        }
Ejemplo n.º 25
0
        public static string ComputeHash(string input)
        {
            // Create a SHA256
            using (System.Security.Cryptography.SHA256 sha256Hash = System.Security.Cryptography.SHA256.Create())
            {
                // ComputeHash - returns byte array
                byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

                return(Convert.ToBase64String(bytes));
            }
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Get the hash for the SHA-256 algorithim.
        /// </summary>
        /// <param name="data">
        /// </param>
        /// <returns>
        /// </returns>
        public byte[] GetHash(byte[] data)
        {
            byte[] hash = null;

            using (System.Security.Cryptography.SHA256 crypto = System.Security.Cryptography.SHA256.Create())
            {
                hash = crypto.ComputeHash(data);
            }

            return(hash);
        }
Ejemplo n.º 27
0
 /// <summary>
 /// Signs a block
 /// </summary>
 public static unsafe byte[] Sign(byte[] src)
 {
     System.Security.Cryptography.SHA256 sha256 = System.Security.Cryptography.SHA256.Create();
     try
     {
         return(sha256.ComputeHash(src));
     }
     finally
     {
         sha256.Dispose();
     }
 }
Ejemplo n.º 28
0
        public string fjos_to_hash(string pass823)
        {
            System.Security.Cryptography.SHA256 sha256 = System.Security.Cryptography.SHA256.Create();

            var pass_in_bytes = sha256.ComputeHash(Encoding.UTF7.GetBytes(pass823));

            //var pass_hash = Encoding.UTF7.GetString(pass_in_bytes);

            var pass_hash = System.Convert.ToBase64String(pass_in_bytes);

            return(pass_hash);
        }
Ejemplo n.º 29
0
        // 將密碼編碼的方法
        private string HashPassword(string str)
        {
            string rethash = "";

            System.Security.Cryptography.SHA256 hash    = System.Security.Cryptography.SHA256.Create();
            System.Text.ASCIIEncoding           encoder = new System.Text.ASCIIEncoding();
            byte[] combined = encoder.GetBytes(str);
            hash.ComputeHash(combined);
            rethash = Convert.ToBase64String(hash.Hash);

            return(rethash);
        }
Ejemplo n.º 30
0
        public static string GetHash(byte[] input)
        {
            System.Security.Cryptography.SHA256        sha256Hash    = System.Security.Cryptography.SHA256.Create();
            System.Security.Cryptography.HashAlgorithm hashAlgorithm = sha256Hash;
            byte[] data     = hashAlgorithm.ComputeHash(input);
            var    sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return(sBuilder.ToString());
        }
Ejemplo n.º 31
0
        /*
         * Validate the checksum of the update zip
         */
        private bool ValidateCheckSum(string refCheckSum, FileInfo file)
        {
            string checkSum = null;

            using (System.IO.FileStream fileStream = System.IO.File.OpenRead(file.FullName))
            {
                using (System.Security.Cryptography.SHA256 sha = System.Security.Cryptography.SHA256.Create())
                {
                    checkSum = BitConverter.ToString(sha.ComputeHash(fileStream)).Replace("-", "");
                }
            }
            return(refCheckSum.ToLowerInvariant().Equals(checkSum.ToLowerInvariant()));
        }