Example #1
0
        public byte[] ComputeHash(byte[] buffer, int offset, int count)
        {
#if WINDOWS_STORE
            MacAlgorithmProvider provider = MacAlgorithmProvider.OpenAlgorithm(algorithmName);

            CryptographicKey hmacKey;
            if (Key != null)
            {
                hmacKey = provider.CreateKey(CryptographicBuffer.CreateFromByteArray(Key));
            }
            else
            {
                hmacKey = provider.CreateKey(CryptographicBuffer.GenerateRandom(provider.MacLength));
            }

            IBuffer hmacValue = CryptographicEngine.Sign(hmacKey, CryptographicBuffer.CreateFromByteArray(buffer));

            byte[] result;
            CryptographicBuffer.CopyToByteArray(hmacValue, out result);

            return(result);
#else
            return(hmac.ComputeHash(buffer, offset, count));
#endif
        }
Example #2
0
        /// <summary>
        /// Calculates OTPs
        /// </summary>
        static long HmacSha1(byte[] key, byte[] value)
        {
            MacAlgorithmProvider provider = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1);
            IBuffer keyMaterial           = CryptographicBuffer.CreateFromByteArray(key);
            var     cKey = provider.CreateKey(keyMaterial);

            byte[] hmacComputedHash;

            IBuffer data   = CryptographicBuffer.CreateFromByteArray(value);
            IBuffer buffer = CryptographicEngine.Sign(cKey, data);

            CryptographicBuffer.CopyToByteArray(buffer, out hmacComputedHash);


            // The RFC has a hard coded index 19 in this value.
            // This is the same thing but also accomodates SHA256 and SHA512
            // hmacComputedHash[19] => hmacComputedHash[hmacComputedHash.Length - 1]

            int offset = hmacComputedHash[hmacComputedHash.Length - 1] & 0xF;

            return
                ((hmacComputedHash[offset] & 0x7f) << 24
                 | (hmacComputedHash[offset + 1]) << 16
                 | (hmacComputedHash[offset + 2]) << 8
                 | (hmacComputedHash[offset + 3]));
        }
Example #3
0
        /*
         * 计算 HMAC-SHA1
         */
        public static String HMACSha1(String data, String key)
        {
            String result = "";

            //使用CodePagesEncodingProvider去注册扩展编码。
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            //注册GBK编码
            Encoding encodingGbk = Encoding.GetEncoding("GBK");

            byte[] dataByte = encodingGbk.GetBytes(data);
            byte[] keyByte  = encodingGbk.GetBytes(key);


            byte[] DataBt = encodingGbk.GetBytes(data);
            byte[] KeyBt  = encodingGbk.GetBytes(key);
            data = Encoding.UTF8.GetString(DataBt);
            key  = Encoding.UTF8.GetString(KeyBt);

            string  SHA1Name                = MacAlgorithmNames.HmacSha1;
            IBuffer buffUtf8Msg             = CryptographicBuffer.ConvertStringToBinary(data, BinaryStringEncoding.Utf8);
            IBuffer buffKeyMaterial         = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
            MacAlgorithmProvider objMacProv = MacAlgorithmProvider.OpenAlgorithm(SHA1Name);
            CryptographicKey     hmacKey    = objMacProv.CreateKey(buffKeyMaterial);
            IBuffer buffHMAC                = CryptographicEngine.Sign(hmacKey, buffUtf8Msg);

            byte[] hashValue = Buffer2Bytes(buffHMAC);
            result = Convert.ToBase64String(hashValue);
            return(result);
        }
        private string _generateConfirmationHashForTime(long time, string tag)
        {
            int n2 = tag != null?Math.Min(40, 8 + tag.Length) : 8;

            var array = new byte[n2];

            for (var n4 = 7; n4 >= 0; n4--)
            {
                array[n4] = (byte)time;
                time    >>= 8;
            }
            if (tag != null)
            {
                Array.Copy(Encoding.UTF8.GetBytes(tag), 0, array, 8, n2 - 8);
            }

            try
            {
                IBuffer identitySecretArray = CryptographicBuffer.DecodeFromBase64String(this.IdentitySecret);

                MacAlgorithmProvider hmacsha1 = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
                CryptographicKey     hmacKey  = hmacsha1.CreateKey(identitySecretArray);

                string encodedData = CryptographicBuffer.EncodeToBase64String(CryptographicEngine.Sign(hmacKey, CryptographicBuffer.CreateFromByteArray(array)));
                string hash        = WebUtility.UrlEncode(encodedData);
                return(hash);
            }
            catch (Exception)
            {
                return(null); //Fix soon: catch-all is BAD!
            }
        }
        private byte[] getMac(byte[] key, byte[] message)
        {
            try
            {
                MacAlgorithmProvider provider = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256);

                IBuffer          buffPrk = CryptographicBuffer.CreateFromByteArray(key);
                CryptographicKey hmacKey = provider.CreateKey(buffPrk);

                IBuffer buffMsg = CryptographicBuffer.CreateFromByteArray(message);

                IBuffer buffHMAC = CryptographicEngine.Sign(hmacKey, buffMsg);

                byte[] ret;
                CryptographicBuffer.CopyToByteArray(buffHMAC, out ret);

                /*Mac mac = Mac.getInstance("HmacSHA256");
                 * mac.init(new SecretKeySpec(key, "HmacSHA256"));*/

                return(ret);
            }
            catch (/*NoSuchAlgorithmException | java.security.InvalidKeyException*/ Exception e) {
                throw new Exception(e.Message);
            }
        }
Example #6
0
        private IBuffer HMac(MacAlgorithmProvider macProv, IBuffer macKey, IBuffer keyMaterial)
        {
            var     saltKey   = macProv.CreateKey(macKey);
            IBuffer hMacValue = CryptographicEngine.Sign(saltKey, keyMaterial);

            return(hMacValue);
        }
Example #7
0
        /// <summary>
        /// Generates a signature using the specified signatureType
        /// </summary>
        /// <param name="url">The full url that needs to be signed including its non OAuth url parameters</param>
        /// <param name="consumerKey">The consumer key</param>
        /// <param name="consumerSecret">The consumer seceret</param>
        /// <param name="token">The token, if available. If not available pass null or an empty string</param>
        /// <param name="tokenSecret">The token secret, if available. If not available pass null or an empty string</param>
        /// <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param>
        /// <param name="signatureType">The type of signature to use</param>
        /// <returns>A base64 string of the hash value</returns>
        public string GenerateSignature(Uri url, string consumerKey, string consumerSecret, string token, string tokenSecret, string httpMethod, string timeStamp, string nonce, SignatureTypes signatureType, out string normalizedUrl, out string normalizedRequestParameters)
        {
            normalizedUrl = null;
            normalizedRequestParameters = null;

            switch (signatureType)
            {
            case SignatureTypes.PLAINTEXT:

                return(Uri.EscapeDataString(string.Format("{0}&{1}", consumerSecret, tokenSecret)));

            case SignatureTypes.HMACSHA1:
                string signatureBase = GenerateSignatureBase(url, consumerKey, token, tokenSecret, httpMethod, timeStamp, nonce, HMACSHA1SignatureType, out normalizedUrl, out normalizedRequestParameters);

                MacAlgorithmProvider hmacsha1 = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
                //HMACSHA1 hmacsha1 = new HMACSHA1();

                // Create a key to be signed with the message.
                BinaryStringEncoding encoding = BinaryStringEncoding.Utf8;
                IBuffer buffKeyMaterial       = CryptographicBuffer.ConvertStringToBinary(string.Format("{0}&{1}", UrlEncode(consumerSecret), string.IsNullOrEmpty(tokenSecret) ? "" : UrlEncode(tokenSecret)), encoding);

                CryptographicKey hmacKey = hmacsha1.CreateKey(buffKeyMaterial);

                return(GenerateSignatureUsingHash(signatureBase, hmacsha1, hmacKey));

            case SignatureTypes.RSASHA1:
                throw new NotImplementedException();

            default:
                throw new ArgumentException("Unknown signature type", "signatureType");
            }
        }
        void CreateHMAC(String strMsg, String strAlgName, out IBuffer buffMsg, out CryptographicKey hmacKey, out IBuffer buffHMAC)
        {
            // Create a MacAlgorithmProvider object for the specified algorithm.
            MacAlgorithmProvider objMacProv = MacAlgorithmProvider.OpenAlgorithm(strAlgName);

            // Demonstrate how to retrieve the name of the algorithm used.
            String strNameUsed = objMacProv.AlgorithmName;

            // Create a buffer that contains the message to be signed.
            BinaryStringEncoding encoding = BinaryStringEncoding.Utf8;

            buffMsg = CryptographicBuffer.ConvertStringToBinary(strMsg, encoding);

            // Create a key to be signed with the message.
            IBuffer buffKeyMaterial = CryptographicBuffer.GenerateRandom(objMacProv.MacLength);

            hmacKey = objMacProv.CreateKey(buffKeyMaterial);

            // Sign the key and message together.
            buffHMAC = CryptographicEngine.Sign(hmacKey, buffMsg);

            // Verify that the HMAC length is correct for the selected algorithm
            if (buffHMAC.Length != objMacProv.MacLength)
            {
                throw new Exception("Error computing digest");
            }
        }
Example #9
0
        /// <summary>
        /// Generate the signature value based on the given signature base and hash algorithm
        /// </summary>
        /// <param name="macAlgorithm">The hash algorithm used to perform the hashing</param>
        /// <param name="signatureBase">The signature based as produced by the GenerateSignatureBase method or by any other means</param>
        /// <param name="key">The key to generate signatrure with macAlgorithm.</param>
        /// <returns>A base64 string of the hash value</returns>
        public string GenerateSignatureUsingHash(MacAlgorithmProvider macAlgorithm, string data, string key)
        {
            if (macAlgorithm == null)
            {
                throw new ArgumentNullException("macAlgorithm");
            }

            if (string.IsNullOrEmpty(data))
            {
                throw new ArgumentNullException("data");
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("key");
            }

            // Convert the key to a buffer.
            IBuffer KeyMaterial = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);

            // Generate key to sign by specified algorithm.
            CryptographicKey MacKey = macAlgorithm.CreateKey(KeyMaterial);

            // Convert the data to a buffer.
            IBuffer DataToBeSigned = CryptographicBuffer.ConvertStringToBinary(data, BinaryStringEncoding.Utf8);

            // Sign the data by specified algorithm with generated key.
            IBuffer SignatureBuffer = CryptographicEngine.Sign(MacKey, DataToBeSigned);

            // Get signature with Base64.
            string signature = CryptographicBuffer.EncodeToBase64String(SignatureBuffer);

            return(signature);
        }
Example #10
0
 public AttachmentCipherInputStream(StorageFile file, byte[] combinedKeyMaterial)
 {
     byte[][]             parts   = Util.split(combinedKeyMaterial, CIPHER_KEY_SIZE, MAC_KEY_SIZE);
     MacAlgorithmProvider mac     = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256);
     IBuffer          keyMaterial = parts[1].AsBuffer();
     CryptographicKey key         = mac.CreateKey(keyMaterial);
 }
Example #11
0
        public string OAuthSignature(string SigBaseString, SignatureAuthType signType)
        {
            string signingKey = string.Empty;

            //Debug.WriteLine("In OAuthSignature function");

            switch (signType)
            {
            case SignatureAuthType.ConsumerSecret:
                signingKey = string.Format("{0}&{1}", OAuth.OAuthTypes.OAuthConsumerSecret, "");
                break;

            case SignatureAuthType.RequestSecretToken:
                signingKey = string.Format("{0}&{1}", OAuth.OAuthTypes.OAuthConsumerSecret, !string.IsNullOrEmpty(OAuth.OAuthTypes.OAuthRequestTokenSecretKey) ? OAuth.OAuthTypes.OAuthRequestTokenSecretKey : String.Empty);
                break;

            case SignatureAuthType.AccessSecretToken:
                signingKey = string.Format("{0}&{1}", OAuth.OAuthTypes.OAuthConsumerSecret, !string.IsNullOrEmpty(OAuth.OAuthTypes.OAuthAccessTokenSecretKey) ? OAuth.OAuthTypes.OAuthAccessTokenSecretKey : String.Empty);
                break;

            default:
                break;
            }

            IBuffer KeyBuffer = CryptographicBuffer.ConvertStringToBinary(signingKey, BinaryStringEncoding.Utf8);
            MacAlgorithmProvider HmacSha1Provider = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1);
            CryptographicKey     MacKey           = HmacSha1Provider.CreateKey(KeyBuffer);
            IBuffer DataToBeSigned  = CryptographicBuffer.ConvertStringToBinary(SigBaseString, BinaryStringEncoding.Utf8);
            IBuffer SignatureBuffer = CryptographicEngine.Sign(MacKey, DataToBeSigned);
            String  Signature       = CryptographicBuffer.EncodeToBase64String(SignatureBuffer);

            //Debug.WriteLine("Signed Signature: " + Signature);
            return(Signature);
        }
Example #12
0
        static IBuffer HMACHandle(IBuffer data, IBuffer key, string hmacType)
        {
            MacAlgorithmProvider hmacAlgorithm = MacAlgorithmProvider.OpenAlgorithm(hmacType);
            CryptographicKey     hmacKey       = hmacAlgorithm.CreateKey(key);

            return(CryptographicEngine.Sign(hmacKey, data));
        }
        private static byte[] ComputeHmacSha1Hash(byte[] data, byte[] key)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            //https://channel9.msdn.com/Forums/TechOff/Porting-to-WinRT/4df7586e1ef5400682eda00f0143b610

            MacAlgorithmProvider macAlgorithmProvider = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
            //BinaryStringEncoding encoding = BinaryStringEncoding.Utf8;

            CryptographicKey hmacKey       = macAlgorithmProvider.CreateKey(key.AsBuffer());
            IBuffer          signedMessage = CryptographicEngine.Sign(hmacKey, data.AsBuffer());

            //var hashedString  = CryptographicBuffer.EncodeToBase64String(signedMessage);

            return(signedMessage.ToArray());


            ////using (var crypto = new System.Security.Cryptography.HMACSHA1(key))
            ////{
            ////    return crypto.ComputeHash(data);
            ////}
        }
Example #14
0
        public SHA1(byte[] hmacKey)
        {
            MacAlgorithmProvider hmacSha1Provider = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
            var materialKey = CryptographicBuffer.CreateFromByteArray(hmacKey);

            macKey = hmacSha1Provider.CreateKey(materialKey);
        }
Example #15
0
        private void CalculateProofs()
        {
            MacAlgorithmProvider  hmac = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
            HashAlgorithmProvider hash = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);

            byte[]           saltedPassword = Hi();
            CryptographicKey hmacKey        = hmac.CreateKey(saltedPassword.AsBuffer());

            // Calculate Client Key
            byte[] clientKey = CryptographicEngine.Sign(hmacKey, _utf.GetBytes("Client Key").AsBuffer()).ToArray();

            // Calculate Server Key
            byte[] serverKey = CryptographicEngine.Sign(hmacKey, _utf.GetBytes("Server Key").AsBuffer()).ToArray();

            // Calculate Stored Key
            byte[] storedKey = hash.HashData(clientKey.AsBuffer()).ToArray();

            var a = new StringBuilder();

            a.Append(_clientFirst);
            a.Append(",");
            a.Append(_utf.GetString(_serverFirst, 0, _serverFirst.Length));
            a.Append(",");
            a.Append(_clientFinal);

            byte[] auth = _utf.GetBytes(a.ToString());

            // Calculate Client Signature
            CryptographicKey storedKeyhmacKey = hmac.CreateKey(storedKey.AsBuffer());

            byte[] signature = CryptographicEngine.Sign(storedKeyhmacKey, auth.AsBuffer()).ToArray();

            // Calculate Server Signature
            CryptographicKey serverKeyhmacKey = hmac.CreateKey(serverKey.AsBuffer());

            _serverSignature = CryptographicEngine.Sign(serverKeyhmacKey, auth.AsBuffer()).ToArray();

            // Calculate Client Proof
            var proof = new byte[20];

            for (int i = 0; i < signature.Length; ++i)
            {
                proof[i] = (byte)(clientKey[i] ^ signature[i]);
            }

            _clientProof = Convert.ToBase64String(proof);
        }
Example #16
0
        public static string HashWith(this string input, MacAlgorithmProvider hashProvider, string key)
        {
            IBuffer          keyMaterial = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
            CryptographicKey cryptoKey   = hashProvider.CreateKey(keyMaterial);
            IBuffer          hash        = CryptographicEngine.Sign(cryptoKey, CryptographicBuffer.ConvertStringToBinary(input, BinaryStringEncoding.Utf8));

            return(CryptographicBuffer.EncodeToBase64String(hash));
        }
Example #17
0
        public SHA1(string hmacKey)
        {
            MacAlgorithmProvider hmacSha1Provider = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
            var inputBuffer = Encoding.GetEncoding("iso-8859-1").GetBytes(hmacKey);
            var materialKey = CryptographicBuffer.CreateFromByteArray(inputBuffer);

            macKey = hmacSha1Provider.CreateKey(materialKey);
        }
Example #18
0
        public OTP(string key)
        {
            MacAlgorithmProvider provider = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1);

            IBuffer keyMaterial = CryptographicBuffer.CreateFromByteArray(key.ToBytesBase32());

            cKey = provider.CreateKey(keyMaterial);
        }
Example #19
0
        /// <summary>
        /// This method uses the Windows Runtime APIs to provide the crypto algorithm.
        /// HMAC computes a Hashed Message Authentication Code with the
        /// crypto hash algorithm as a parameter.
        /// </summary>
        /// <param name="crypto">The crypto algorithm.</param>
        /// <param name="keyBytes">The bytes to use for the HMAC key.</param>
        /// <param name="text">The message or text to be authenticated.</param>
        /// <returns></returns>
        public static byte[] HmacSha(MacAlgorithmEnum crypto, byte[] keyBytes,
                                     byte[] text)
        {
            // Select crypto algorithm
            MacAlgorithmProvider macAlgorithmProvider = null;

            switch (crypto)
            {
            case MacAlgorithmEnum.HmacSha1:
                macAlgorithmProvider = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1);
                break;

            case MacAlgorithmEnum.HmacSha256:
                macAlgorithmProvider = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256);
                break;

            case MacAlgorithmEnum.HmacSha512:
                macAlgorithmProvider = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha512);
                break;
            }
            if (macAlgorithmProvider == null)
            {
                throw new InvalidOperationException("MacAlgorithmProvider failed to initialize.");
            }
            // Get buffer
            var keyBuffer = CryptographicBuffer.CreateFromByteArray(keyBytes);

            if (keyBuffer == null)
            {
                throw new InvalidOperationException("Invaild Key buffer.");
            }
            var key = macAlgorithmProvider.CreateKey(keyBuffer);

            if (key == null)
            {
                throw new InvalidOperationException("Invaild Key.");
            }

            // Get hash
            var dataBuffer = CryptographicBuffer.CreateFromByteArray(text);

            if (dataBuffer == null)
            {
                throw new InvalidOperationException("Invaild data buffer.");
            }

            // Sign hash
            var dataBufferSigned = CryptographicEngine.Sign(key, dataBuffer);

            if (dataBufferSigned == null)
            {
                throw new InvalidOperationException("Invaild signed data.");
            }

            byte[] hashBytes;
            CryptographicBuffer.CopyToByteArray(dataBufferSigned, out hashBytes);
            return(hashBytes);
        }
        public static string GenerateHash(string input, string key)
        {
            MacAlgorithmProvider mac     = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1);
            IBuffer          keyMaterial = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
            CryptographicKey cryptoKey   = mac.CreateKey(keyMaterial);
            IBuffer          hash        = CryptographicEngine.Sign(cryptoKey, CryptographicBuffer.ConvertStringToBinary(input, BinaryStringEncoding.Utf8));

            return(CryptographicBuffer.EncodeToBase64String(hash));
        }
Example #21
0
        protected override string GenerateSignatureUsingHash(string data, byte[] key)
        {
            MacAlgorithmProvider hmacsha1 = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1);
            IBuffer          dataToSign   = Strings.AsciiStringToBytes(data).AsBuffer();
            CryptographicKey cryptoKey    = hmacsha1.CreateKey(key.AsBuffer());
            IBuffer          signature    = CryptographicEngine.Sign(cryptoKey, dataToSign);

            return(CryptographicBuffer.EncodeToBase64String(signature));
        }
Example #22
0
        public static string HashWith(this string input, byte[] keyBytes, MacAlgorithmProvider algorithm)
        {
            var messageBytes = Encoding.UTF8.GetBytes(input);
            MacAlgorithmProvider objMacProv = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
            CryptographicKey     hmacKey    = algorithm.CreateKey(keyBytes.AsBuffer());
            IBuffer buffHMAC = CryptographicEngine.Sign(hmacKey, messageBytes.AsBuffer());

            return(CryptographicBuffer.EncodeToBase64String(buffHMAC));
        }
Example #23
0
        public static byte[] HmacSha1Sign(byte[] keyBytes, string message)
        {
            var messageBytes = Encoding.UTF8.GetBytes(message);
            MacAlgorithmProvider objMacProv = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
            CryptographicKey     hmacKey    = objMacProv.CreateKey(keyBytes.AsBuffer());
            IBuffer buffHMAC = CryptographicEngine.Sign(hmacKey, messageBytes.AsBuffer());

            return(buffHMAC.ToArray());
        }
Example #24
0
        internal string HashWith(string input, string key)
        {
            MacAlgorithmProvider mac     = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
            IBuffer          keyMaterial = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
            CryptographicKey cryptoKey   = mac.CreateKey(keyMaterial);
            //IBuffer hash = hashProvider.HashData(CryptographicBuffer.ConvertStringToBinary(input, BinaryStringEncoding.Utf8));
            IBuffer hash = CryptographicEngine.Sign(cryptoKey, CryptographicBuffer.ConvertStringToBinary(input, BinaryStringEncoding.Utf8));

            return(CryptographicBuffer.EncodeToBase64String(hash));
        }
Example #25
0
        public byte[] ComputeHmacSha256(byte[] secretKey, byte[] data)
        {
            const string HmacSha256AlgorithmName = "HMAC_SHA256";

            MacAlgorithmProvider provider = MacAlgorithmProvider.OpenAlgorithm(HmacSha256AlgorithmName);
            var key    = provider.CreateKey(secretKey.AsBuffer());
            var hashed = CryptographicEngine.Sign(key, data.AsBuffer());

            return(hashed.ToArray());
        }
Example #26
0
        public static string SignString(string key, string message)
        {
            MacAlgorithmProvider macAlgorithmProvider = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA256");
            var binaryMessage       = CryptographicBuffer.ConvertStringToBinary(message, BinaryStringEncoding.Utf8);
            var binaryKeyMaterial   = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
            var hmacKey             = macAlgorithmProvider.CreateKey(binaryKeyMaterial);
            var binarySignedMessage = CryptographicEngine.Sign(hmacKey, binaryMessage);
            var signedMessage       = CryptographicBuffer.EncodeToHexString(binarySignedMessage);

            return(signedMessage);
        }
        public string GetSignature(string sigBaseString, string consumerSecretKey)
        {
            IBuffer KeyMaterial = CryptographicBuffer.ConvertStringToBinary(consumerSecretKey + "&", BinaryStringEncoding.Utf8);
            MacAlgorithmProvider HmacSha1Provider = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
            CryptographicKey     MacKey           = HmacSha1Provider.CreateKey(KeyMaterial);
            IBuffer DataToBeSigned  = CryptographicBuffer.ConvertStringToBinary(sigBaseString, BinaryStringEncoding.Utf8);
            IBuffer SignatureBuffer = CryptographicEngine.Sign(MacKey, DataToBeSigned);
            string  Signature       = CryptographicBuffer.EncodeToBase64String(SignatureBuffer);

            return(Signature);
        }
        /**
         * Signature of Request to be authorized of doing Request
         * @param SigBaseString URL To Sign
         * @param tokenSecret Token Secret to Sign
         */
        public String SignMeThat(String SigBaseString, String tokenSecret)
        {
            IBuffer KeyMaterial = CryptographicBuffer.ConvertStringToBinary(API_SECRET + "&" + tokenSecret, BinaryStringEncoding.Utf8);
            MacAlgorithmProvider HmacSha1Provider = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
            CryptographicKey     MacKey           = HmacSha1Provider.CreateKey(KeyMaterial);
            IBuffer DataToBeSigned  = CryptographicBuffer.ConvertStringToBinary(SigBaseString, BinaryStringEncoding.Utf8);
            IBuffer SignatureBuffer = CryptographicEngine.Sign(MacKey, DataToBeSigned);
            String  Signature       = CryptographicBuffer.EncodeToBase64String(SignatureBuffer);

            return(Signature);
        }
Example #29
0
 public string GetSignature(string sigBaseString, string consumerSecretKey, string requestTokenSecretKey = null)
 {
     var signingKey = string.Format("{0}&{1}", consumerSecretKey, !string.IsNullOrEmpty(requestTokenSecretKey) ? requestTokenSecretKey : "");
     IBuffer keyMaterial = CryptographicBuffer.ConvertStringToBinary(signingKey, BinaryStringEncoding.Utf8);
     MacAlgorithmProvider hmacSha1Provider = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
     CryptographicKey macKey = hmacSha1Provider.CreateKey(keyMaterial);
     IBuffer dataToBeSigned = CryptographicBuffer.ConvertStringToBinary(sigBaseString, BinaryStringEncoding.Utf8);
     IBuffer signatureBuffer = CryptographicEngine.Sign(macKey, dataToBeSigned);
     String signature = CryptographicBuffer.EncodeToBase64String(signatureBuffer);
     return signature;
 }
Example #30
0
        public static string HmacMd5(byte[] secretKey, byte[] data)
        {
            string str_alg_name = MacAlgorithmNames.HmacMd5;
            MacAlgorithmProvider obj_mac_prov  = MacAlgorithmProvider.OpenAlgorithm(str_alg_name);
            IBuffer          buff_msg          = CryptographicBuffer.CreateFromByteArray(data);
            IBuffer          buff_key_material = CryptographicBuffer.CreateFromByteArray(secretKey);
            CryptographicKey hmac_key          = obj_mac_prov.CreateKey(buff_key_material);
            IBuffer          hmac = CryptographicEngine.Sign(hmac_key, buff_msg);

            return(CryptographicBuffer.EncodeToBase64String(hmac));
        }