/// <summary> /// SignRequest /// </summary> /// <param name="request"></param> /// <param name="body"></param> /// <returns></returns> public string SignRequest(System.Net.HttpWebRequest request, byte[] body) { Uri u = request.RequestUri; var algorithm = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithm.HmacSha1); CryptographicHash hasher = algorithm.CreateHash(secretKey); hasher.Append(body); byte[] mac = hasher.GetValueAndReset(); string macBase64 = Convert.ToBase64String(mac); string pathAndQuery = request.RequestUri.PathAndQuery; byte[] pathAndQueryBytes = Config.Encoding.GetBytes(pathAndQuery); using (MemoryStream buffer = new MemoryStream()) { buffer.Write(pathAndQueryBytes, 0, pathAndQueryBytes.Length); buffer.WriteByte((byte)'\n'); if (body.Length > 0) { buffer.Write(body, 0, body.Length); } hasher.Dispose(); return(this.accessKey + ":" + macBase64); } }
private static bool VerifyHMAC(IBuffer data, IBuffer hmacKeyBuffer, IBuffer hmac) { var provider = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256); var key = provider.CreateKey(hmacKeyBuffer); return(CryptographicEngine.VerifySignature(key, data, hmac)); }
private static IBuffer ComputeHMAC(IBuffer data, IBuffer hmacKeyBuffer) { var provider = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256); var key = provider.CreateKey(hmacKeyBuffer); return(CryptographicEngine.Sign(key, 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); ////} }
public SHA1(byte[] hmacKey) { MacAlgorithmProvider hmacSha1Provider = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1"); var materialKey = CryptographicBuffer.CreateFromByteArray(hmacKey); macKey = hmacSha1Provider.CreateKey(materialKey); }
/// <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])); }
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 }
private IBuffer HMac(MacAlgorithmProvider macProv, IBuffer macKey, IBuffer keyMaterial) { var saltKey = macProv.CreateKey(macKey); IBuffer hMacValue = CryptographicEngine.Sign(saltKey, keyMaterial); return(hMacValue); }
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! } }
public string PubnubAccessManagerSign(string key, string data) { string secret = key; string message = data; var encoding = new System.Text.UTF8Encoding(); byte[] keyByte = encoding.GetBytes(secret); byte[] messageBytes = encoding.GetBytes(message); #if NETFX_CORE var hmacsha256 = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256); IBuffer valueBuffer = CryptographicBuffer.ConvertStringToBinary(message, BinaryStringEncoding.Utf8); IBuffer buffKeyMaterial = CryptographicBuffer.ConvertStringToBinary(secret, BinaryStringEncoding.Utf8); CryptographicKey cryptographicKey = hmacsha256.CreateKey(buffKeyMaterial); // Sign the key and message together. IBuffer bufferProtected = CryptographicEngine.Sign(cryptographicKey, valueBuffer); DataReader dataReader = DataReader.FromBuffer(bufferProtected); byte[] hashmessage = new byte[bufferProtected.Length]; dataReader.ReadBytes(hashmessage); return(Convert.ToBase64String(hashmessage).Replace('+', '-').Replace('/', '_')); #else using (var hmacsha256 = new HMACSHA256(keyByte)) { byte[] hashmessage = hmacsha256.ComputeHash(messageBytes); return(Convert.ToBase64String(hashmessage).Replace('+', '-').Replace('/', '_')); } #endif }
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); }
/// <summary> /// Create a HmacSha256 encoded string. Windows IoT/WP is missing the default methods (System.Security.Cryptography.HMACSHA256) /// </summary> /// <param name="SecretKey">The secret key used to encrypt the provided string</param> /// <param name="Value">The value to be encrypted</param> /// <returns></returns> private string HmacSha256(string SecretKey, string Value) { // Move strings to buffers. // IoT SAS keys needs a different key convertion (??) IBuffer key = null; switch (EventHubType) { case EventHubType.IoTHub: key = Convert.FromBase64String(SecretKey).AsBuffer(); break; default: key = CryptographicBuffer.ConvertStringToBinary(SecretKey, BinaryStringEncoding.Utf8); break; } var msg = CryptographicBuffer.ConvertStringToBinary(Value, BinaryStringEncoding.Utf8); // Create HMAC. var objMacProv = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256); var hash = objMacProv.CreateHash(key); hash.Append(msg); return(CryptographicBuffer.EncodeToBase64String(hash.GetValueAndReset())); }
public void Initialize() { var buffer = CryptographicBuffer.CreateFromByteArray(Key); algorithm = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacMd5); hash = algorithm.CreateHash(buffer); }
public byte[] ComputeHash(byte[] buffer) { var crypt = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1"); var sigBuffer = CryptographicEngine.Sign(crypt.CreateKey(CryptographicBuffer.CreateFromByteArray(hmackey)), CryptographicBuffer.CreateFromByteArray(buffer)); return(sigBuffer.ToArray()); }
public async Task <bool> CompareAsync(byte[] a, byte[] b) { var provider = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithm.HmacSha256); var hasher = provider.CreateHash(await RandomBytesAsync(32)); hasher.Append(a); var mac1 = hasher.GetValueAndReset(); hasher.Append(b); var mac2 = hasher.GetValueAndReset(); if (mac1.Length != mac2.Length) { return(false); } for (int i = 0; i < mac2.Length; i++) { if (mac1[i] != mac2[i]) { return(false); } } return(true); }
/// <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"); } }
/// <summary> /// HTTP请求签名 /// </summary> /// <param name="url">请求目标的URL</param> /// <param name="body">请求的主体数据</param> /// <returns></returns> public string SignRequest(string url, byte[] body) { Uri u = new Uri(url); string pathAndQuery = u.PathAndQuery; byte[] pathAndQueryBytes = Encoding.UTF8.GetBytes(pathAndQuery); using (MemoryStream buffer = new MemoryStream()) { buffer.Write(pathAndQueryBytes, 0, pathAndQueryBytes.Length); buffer.WriteByte((byte)'\n'); if (body != null && body.Length > 0) { buffer.Write(body, 0, body.Length); } #if WINDOWS_UWP var hma = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1); var skBuffer = CryptographicBuffer.ConvertStringToBinary(mac.SecretKey, BinaryStringEncoding.Utf8); var hmacKey = hma.CreateKey(skBuffer); var dataBuffer = CryptographicBuffer.CreateFromByteArray(buffer.ToArray()); var signBuffer = CryptographicEngine.Sign(hmacKey, dataBuffer); byte[] digest; CryptographicBuffer.CopyToByteArray(signBuffer, out digest); #else HMACSHA1 hmac = new HMACSHA1(Encoding.UTF8.GetBytes(mac.SecretKey)); byte[] digest = hmac.ComputeHash(buffer.ToArray()); #endif string digestBase64 = Base64.UrlSafeBase64Encode(digest); return(string.Format("{0}:{1}", mac.AccessKey, digestBase64)); } }
public byte[] HMACSignBinary(byte[] data, byte[] key, SigningAlgorithm algorithmName) { var crypt = MacAlgorithmProvider.OpenAlgorithm(ConvertToAlgorithName(algorithmName)); if (key == null || key.Length == 0) { throw new ArgumentNullException("key", "Please specify a Secret Signing Key."); } if (data == null || data.Length == 0) { throw new ArgumentNullException("data", "Please specify data to sign."); } if (null == crypt) { throw new ArgumentNullException("algorithm", "Please specify a KeyedHashAlgorithm to use."); } IBuffer dataBuffer = CryptographicBuffer.CreateFromByteArray(data); var keyBuffer = CryptographicBuffer.CreateFromByteArray(key); var cryptoKey = crypt.CreateKey(keyBuffer); var sigBuffer = CryptographicEngine.Sign(cryptoKey, dataBuffer); return(sigBuffer.ToArray()); }
private byte[] Hi() { var prev = new byte[20]; // Add 1 to the end of salt with most significat octet first var key = new byte[_salt.Length + 4]; Array.Copy(_salt, key, _salt.Length); byte[] g = { 0, 0, 0, 1 }; Array.Copy(g, 0, key, _salt.Length, 4); // Compute initial hash var hmac = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1"); // Create Key var passwordBuffer = CryptographicBuffer.ConvertStringToBinary(Stringprep.SASLPrep(Password), BinaryStringEncoding.Utf8); var hmacKey = hmac.CreateKey(passwordBuffer); var result = CryptographicEngine.Sign(hmacKey, key.AsBuffer()).ToArray(); Array.Copy(result, prev, (int)result.Length); for (var i = 1; i < _i; ++i) { var temp = CryptographicEngine.Sign(hmacKey, prev.AsBuffer()).ToArray(); for (var j = 0; j < temp.Length; ++j) { result[j] ^= temp[j]; } Array.Copy(temp, prev, temp.Length); } return(result); }
public string HMACSign(byte[] data, string key, SigningAlgorithm algorithmName) { var crypt = MacAlgorithmProvider.OpenAlgorithm(ConvertToAlgorithName(algorithmName)); if (String.IsNullOrEmpty(key)) { throw new ArgumentNullException("key", "Please specify a Secret Signing Key."); } if (data == null || data.Length == 0) { throw new ArgumentNullException("data", "Please specify data to sign."); } if (null == crypt) { throw new ArgumentNullException("algorithm", "Please specify a KeyedHashAlgorithm to use."); } IBuffer dataBuffer = CryptographicBuffer.CreateFromByteArray(data); var keyBuffer = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8); var cryptoKey = crypt.CreateKey(keyBuffer); var sigBuffer = CryptographicEngine.Sign(cryptoKey, dataBuffer); string signature = CryptographicBuffer.EncodeToBase64String(sigBuffer); return(signature); }
/// <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); }
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"); } }
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); }
/* * 计算 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); }
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 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); } }
/// <summary> /// Initializes a new instance of the <see cref="MailKit.Security.KeyedHashAlgorithm"/> class. /// </summary> /// <remarks> /// Creates a new keyed hash algorithm context. /// </remarks> /// <param name="algorithm">The MAC algorithm name.</param> /// <param name="key">The secret key.</param> protected KeyedHashAlgorithm(string algorithm, byte[] key) { var mac = MacAlgorithmProvider.OpenAlgorithm(algorithm); var buf = CryptographicBuffer.CreateFromByteArray(key); hmac = mac.CreateHash(buf); }
public Task <byte[]> HmacAsync(byte[] value, byte[] key, CryptoHashAlgorithm algorithm) { var provider = MacAlgorithmProvider.OpenAlgorithm(ToMacAlgorithm(algorithm)); var hasher = provider.CreateHash(key); hasher.Append(value); return(Task.FromResult(hasher.GetValueAndReset())); }
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)); }
private string GetSHA1Key(byte[] secretKey, string value) { var objMacProv = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1); var hash = objMacProv.CreateHash(secretKey.AsBuffer()); hash.Append(CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8)); return(CryptographicBuffer.EncodeToBase64String(hash.GetValueAndReset()).Replace('+', '-').Replace('/', '_')); }
public KeyedHashAlgorithm(HashAlgorithmNames algorithm) { switch(algorithm) { case HashAlgorithmNames.Sha1: alg = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1); break; case HashAlgorithmNames.Sha256: alg = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256); break; case HashAlgorithmNames.Sha384: alg = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha384); break; case HashAlgorithmNames.Sha512: alg = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha512); break; case HashAlgorithmNames.Md5: alg = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacMd5); break; default: throw new NotSupportedException(); } }