示例#1
0
        private Dictionary <string, object> GetHttpHeaders(string endpoint, string payload, string access_token, string secret_key)
        {
            var _signature = "";

            var _secretKey = Encoding.UTF8.GetBytes(secret_key.ToUpper());

            using (var _hmac = new HMACSHA512(_secretKey))
            {
                _hmac.Initialize();

                var _bytes   = Encoding.UTF8.GetBytes(payload);
                var _rawHmac = _hmac.ComputeHash(_bytes);

                _signature = BytesToHex(_rawHmac);
            }

            var _headers = new Dictionary <string, object>();

            {
                _headers.Add("X-COINONE-PAYLOAD", payload);
                _headers.Add("X-COINONE-SIGNATURE", _signature);
            }

            return(_headers);
        }
示例#2
0
        private Dictionary <string, object> GetHttpHeaders(string endpoint, Dictionary <string, object> rgData, string apiKey, string apiSecret)
        {
            var _nonce   = CUnixTime.NowMilli.ToString();
            var _data    = EncodeURIComponent(rgData);
            var _message = String.Format("{0};{1};{2}", endpoint, _data, _nonce);

            var _secretKey = Encoding.UTF8.GetBytes(apiSecret);
            var _hmac      = new HMACSHA512(_secretKey);

            _hmac.Initialize();

            var _bytes   = Encoding.UTF8.GetBytes(_message);
            var _rawHmac = _hmac.ComputeHash(_bytes);

            var _encoded   = EncodeHex(_rawHmac);
            var _signature = Convert.ToBase64String(_encoded);

            var _headers = new Dictionary <string, object>();

            {
                _headers.Add("Api-Key", apiKey);
                _headers.Add("Api-Sign", _signature);
                _headers.Add("Api-Nonce", _nonce);
            }

            return(_headers);
        }
示例#3
0
        /// <summary>
        /// NetAESEncryption constructor
        /// </summary>
        public NetAESEncryption(string key, int bitsize)
        {
            if (!s_keysizes.Contains(bitsize))
            {
                throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_keysizes)));
            }

            byte[] entropy = Encoding.UTF32.GetBytes(key);
            // I know hardcoding salts is bad, but in this case I think it is acceptable.
            HMACSHA512 hmacsha512 = new HMACSHA512(Convert.FromBase64String("i88NEiez3c50bHqr3YGasDc4p8jRrxJAaiRiqixpvp4XNAStP5YNoC2fXnWkURtkha6M8yY901Gj07IRVIRyGL=="));

            hmacsha512.Initialize();
            for (int i = 0; i < 1000; i++)
            {
                entropy = hmacsha512.ComputeHash(entropy);
            }
            int keylen = bitsize / 8;

            m_key = new byte[keylen];
            Buffer.BlockCopy(entropy, 0, m_key, 0, keylen);
            m_iv = new byte[s_blocksizes[0] / 8];

            Buffer.BlockCopy(entropy, entropy.Length - m_iv.Length - 1, m_iv, 0, m_iv.Length);
            m_bitSize = bitsize;
        }
        /// <summary>
        /// NetTriplsDESEncryption constructor
        /// </summary>
        public NetTripleDESEncryption(string key, int bitsize)
        {
            if (!m_keysizes.Contains(bitsize))
            {
                string lengths = m_keysizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i));
                lengths = lengths.Remove(lengths.Length - 3);
                throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", lengths));
            }
            byte[] entropy = Encoding.UTF32.GetBytes(key);
            // I know hardcoding salts is bad, but in this case I think it is acceptable.
            HMACSHA512 hmacsha512 = new HMACSHA512(Convert.FromBase64String("i88NEiez3c50bHqr3YGasDc4p8jRrxJAaiRiqixpvp4XNAStP5YNoC2fXnWkURtkha6M8yY901Gj07IRVIRyGL=="));

            hmacsha512.Initialize();
            for (int i = 0; i < 1000; i++)
            {
                entropy = hmacsha512.ComputeHash(entropy);
            }
            int keylen = bitsize / 8;

            m_key = new byte[keylen];
            Buffer.BlockCopy(entropy, 0, m_key, 0, keylen);
            m_iv = new byte[m_blocksizes[0] / 8];

            Buffer.BlockCopy(entropy, entropy.Length - m_iv.Length - 1, m_iv, 0, m_iv.Length);
            m_bitSize = bitsize;
        }
示例#5
0
        public string SingData(string signatureString)
        {
            hmac.Initialize();

            byte[] buffer = Encoding.UTF8.GetBytes(signatureString);

            return(BitConverter.ToString(hmac.ComputeHash(buffer)).Replace("-", "").ToLower());
        }
示例#6
0
 public byte[] Hash(byte[] clearBytes)
 {
     using (var hmac = new HMACSHA512(_key))
     {
         hmac.Initialize();
         byte[] hashBytes = hmac.ComputeHash(clearBytes);
         return(hashBytes);
     }
 }
示例#7
0
        public static string SingRestData(string secretKey, string signatureString)
        {
            var        enc  = Encoding.UTF8;
            HMACSHA512 hmac = new HMACSHA512(enc.GetBytes(secretKey));

            hmac.Initialize();

            byte[] buffer = enc.GetBytes(signatureString);

            return(BitConverter.ToString(hmac.ComputeHash(buffer)).Replace("-", "").ToLower());
        }
        private byte[] HmacSha512(byte[] key, params byte[][] authenticateMe)
        {
            using var ms         = new MemoryStream();
            using var hmacSha512 = new HMACSHA512(key);
            hmacSha512.Initialize();
            foreach (var bytes in authenticateMe)
            {
                ms.Write(bytes);
            }

            return(hmacSha512.ComputeHash(ms.ToArray()));
        }
示例#9
0
        private string StringToHmacSHA512(string queryString)
        {
            using (var hmac = new HMACSHA512())
            {
                hmac.Key = _key;

                hmac.Initialize();
                var buffer         = Encoding.ASCII.GetBytes(queryString);
                var signatureBytes = hmac.ComputeHash(buffer);
                var signature      = Convert.ToBase64String(signatureBytes);
                return(signature);
            }
        }
示例#10
0
 public static string HMACSHA512(string input, string key)
 {
     byte[] secrectKey = Encoding.UTF8.GetBytes(key);
     using (HMACSHA512 hmac = new HMACSHA512(secrectKey))
     {
         hmac.Initialize();
         byte[] bytes_hmac_in  = Encoding.UTF8.GetBytes(input);
         byte[] bytes_hamc_out = hmac.ComputeHash(bytes_hmac_in);
         string str_hamc_out   = BitConverter.ToString(bytes_hamc_out);
         str_hamc_out = str_hamc_out.Replace("-", "");
         return(str_hamc_out);
     }
 }
        public static StringBuilder AppendSignature(this StringBuilder builder, string key)
        {
            using (var hmac = new HMACSHA512())
            {
                hmac.Key = Convert.FromBase64String(key);

                hmac.Initialize();
                var buffer         = Encoding.ASCII.GetBytes(builder.ToString());
                var signatureBytes = hmac.ComputeHash(buffer);
                builder.Append($"&Signature={HttpUtility.UrlEncode(Convert.ToBase64String(signatureBytes))}");
                return(builder);
            }
        }
示例#12
0
        public static HdKey GetMasterKeyFromSeed(ReadOnlySpan <byte> seed)
        {
            var key = Encoding.UTF8.GetBytes(Ed25519Curve).ToList();

            using var hash = new HMACSHA512(key.ToArray());
            hash.Initialize();
            hash.ComputeHash(seed.ToArray());
            var i = hash.Hash.AsSpan();

            return(new HdKey
            {
                Key = i.Slice(0, 32).ToArray(),
                ChainCode = i.Slice(32).ToArray()
            });
        }
示例#13
0
        private string CalculateDigest(string applicationId, string secret, string token)
        {
            // The hmac salt is the concatenation of application ID and secret to eliminate the use of lookup table for brute force attacks.
            string salt = applicationId + secret;

            byte[]     secretKeyBArr = Encoding.UTF8.GetBytes(salt);
            byte[]     tokenBArr     = Encoding.UTF8.GetBytes(token);
            HMACSHA512 hmacsha512    = new HMACSHA512(secretKeyBArr);

            hmacsha512.Initialize();

            byte[] final = hmacsha512.ComputeHash(tokenBArr);

            return(Convert.ToBase64String(final, Base64FormattingOptions.None));
        }
        public static string HashPassword(string secret, string password)
        {
            using var hmac = new HMACSHA512
                  {
                      Key = Encoding.UTF8.GetBytes(secret)
                  };
            hmac.Initialize();
            var pwdHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(password));
            var sb      = new StringBuilder();

            foreach (var b in pwdHash)
            {
                sb.Append(b.ToString("X"));
            }

            return(sb.ToString());
        }
示例#15
0
        public static Object hash_hmac(string signatureString, string secretKey, bool raw_output = false)
        {
            var        enc  = Encoding.UTF8;
            HMACSHA512 hmac = new HMACSHA512(enc.GetBytes(secretKey));

            hmac.Initialize();

            byte[] buffer = enc.GetBytes(signatureString);
            if (raw_output)
            {
                return(hmac.ComputeHash(buffer));
            }
            else
            {
                return(BitConverter.ToString(hmac.ComputeHash(buffer)).Replace("-", "").ToLower());
            }
        }
示例#16
0
        /// <summary>
        /// HMAC_SHA512
        /// </summary>
        /// <param name="srcString">The string to be encrypted</param>
        /// <param name="key">encrypte key</param>
        /// <returns></returns>
        public static string HMACSHA512(string srcString, string key)
        {
            Check.Argument.IsNotEmpty(srcString, nameof(srcString));
            Check.Argument.IsNotEmpty(key, nameof(key));

            byte[] secrectKey = Encoding.UTF8.GetBytes(key);
            using (HMACSHA512 hmac = new HMACSHA512(secrectKey))
            {
                hmac.Initialize();

                byte[] bytes_hmac_in  = Encoding.UTF8.GetBytes(srcString);
                byte[] bytes_hamc_out = hmac.ComputeHash(bytes_hmac_in);

                string str_hamc_out = BitConverter.ToString(bytes_hamc_out);
                str_hamc_out = str_hamc_out.Replace("-", "");

                return(str_hamc_out);
            }
        }
示例#17
0
        public override void Initialize()
        {
            aesNonce = new byte[12];
            aesKey   = new byte[32];

            RandomNumberGenerator.Fill(aesNonce);
            RandomNumberGenerator.Fill(aesKey);

            var tasks = new Task[options.Threads];

            for (var i = 0; i < options.Threads; i++)
            {
                var i1 = i;

                tasks[i1] = Task.Run(() =>
                {
                    var data  = Encoding.UTF8.GetBytes(DataGenerator.GenerateString((int)(volume / options.Threads)));
                    var rand  = new Random();
                    sha512Key = new byte[64];

                    for (var j = 0; j < 64; j++)
                    {
                        sha512Key[j] = (byte)rand.Next();
                    }

                    var hmac = new HMACSHA512(sha512Key);
                    hmac.Initialize();

                    datasSHA[i1] = Encoding.Default.GetString(hmac.ComputeHash(data));

                    datasAES[i1] = new byte[data.Length];

                    using var aes = new AesGcm(aesKey);
                    aesTag[i1]    = new byte[16];

                    aes.Encrypt(aesNonce, data, datasAES[i1], aesTag[i1]);
                });
            }

            Task.WaitAll(tasks);
            GC.Collect();
        }
示例#18
0
        /// <summary>
        /// HMAC_SHA512
        /// </summary>
        /// <param name="source">The string to be encrypted</param>
        /// <param name="key">encrypte key</param>
        /// <returns></returns>
        public static string A512(string source, string key)
        {
            if (string.IsNullOrEmpty(source))
            {
                throw new ArgumentNullException(nameof(source), "");
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException(nameof(key), "");
            }

            using (var hmac = new HMACSHA512(Encoding.UTF8.GetBytes(key)))
            {
                hmac.Initialize();
                var strHmacOut = BitConverter.ToString(hmac.ComputeHash(Encoding.UTF8.GetBytes(source)));
                strHmacOut = strHmacOut.Replace("-", "");
                return(strHmacOut);
            }
        }