public static string HashWith(this string input, System.Security.Cryptography.HMAC algorithm) { var data = Encoding.UTF8.GetBytes(input); var hash = algorithm.ComputeHash(data); return(Convert.ToBase64String(hash)); }
public static void InitRC4Encryption(byte[] secretKey, byte[] pubKeyIn, int inOffset, byte[] pubKeyOut, int outOffset, out byte[] rc4keyIn, out byte[] rc4keyOut) { byte[] digest = new byte[RTMPConst.SHA256_DIGEST_LENGTH]; System.Security.Cryptography.HMAC hmac = System.Security.Cryptography.HMACSHA256.Create("HMACSHA256"); hmac.Key = secretKey; byte[] actualpubKeyIn = new byte[128]; Array.Copy(pubKeyIn, inOffset, actualpubKeyIn, 0, 128); digest = hmac.ComputeHash(actualpubKeyIn); rc4keyOut = new byte[16]; Array.Copy(digest, rc4keyOut, 16); LibRTMPLogger.Log(LibRTMPLogLevel.Trace, "[CDR.LibRTMP.RTMPHelper] RC4 Out Key: "); LibRTMPLogger.LogHex(LibRTMPLogLevel.Trace, rc4keyOut, 0, 16); hmac = System.Security.Cryptography.HMACSHA256.Create("HMACSHA256"); hmac.Key = secretKey; byte[] actualpubKeyOut = new byte[128]; Array.Copy(pubKeyOut, outOffset, actualpubKeyOut, 0, 128); digest = hmac.ComputeHash(actualpubKeyOut); rc4keyIn = new byte[16]; Array.Copy(digest, rc4keyIn, 16); LibRTMPLogger.Log(LibRTMPLogLevel.Trace, "[CDR.LibRTMP.RTMPHelper] RC4 In Key: "); LibRTMPLogger.LogHex(LibRTMPLogLevel.Trace, rc4keyIn, 0, 16); }
public HmacHashCrypto(HashAlgName algorithmName, byte[] key) { AssertUtil.ArgumentNotEmpty(key, nameof(key)); this.AlgorithmName = algorithmName; this.Key = key; this.provider = CreateProvider(algorithmName, key); }
public static void HMACsha256(byte[] message, int messageOffset, int messageLen, byte[] key, int keylen, byte[] digest, int digestOffset) { System.Security.Cryptography.HMAC hmac = System.Security.Cryptography.HMACSHA256.Create("HMACSHA256"); byte[] actualKey = new byte[keylen]; Array.Copy(key, actualKey, keylen); hmac.Key = actualKey; byte[] actualMessage = new byte[messageLen]; Array.Copy(message, messageOffset, actualMessage, 0, messageLen); byte[] calcDigest = hmac.ComputeHash(actualMessage); Array.Copy(calcDigest, 0, digest, digestOffset, calcDigest.Length); }
public void Dispose() { if (this.IsDisposed) { return; } this.IsDisposed = true; this.provider.Dispose(); this.provider = null; }
public void init(byte[] key) { if (key.Length > 20) { byte[] tmp = new byte[20]; Array.Copy(key, 0, tmp, 0, 20); key = tmp; } // SecretKeySpec skey=new SecretKeySpec(key, "HmacSHA1"); // mac=Mac.getInstance("HmacSHA1"); // mac.init(skey); mentalis_mac = new System.Security.Cryptography.HMACMD5(key); cs = new System.Security.Cryptography.CryptoStream(System.IO.Stream.Null, mentalis_mac, System.Security.Cryptography.CryptoStreamMode.Write); }
public void init(byte[] key) { if (key.Length > 20) { byte[] tmp = new byte[20]; Array.Copy(key, 0, tmp, 0, 20); key = tmp; } // SecretKeySpec skey=new SecretKeySpec(key, "HmacSHA1"); // mac=Mac.getInstance("HmacSHA1"); // mac.init(skey); mentalis_mac = new System.Security.Cryptography.HMACMD5(key); cs = new System.Security.Cryptography.CryptoStream(System.IO.Stream.Null, mentalis_mac, System.Security.Cryptography.CryptoStreamMode.Write); }
public void init(byte[] key) { if (key.Length > bsize) { byte[] tmp = new byte[bsize]; Array.Copy(key, 0, tmp, 0, bsize); key = tmp; } // SecretKeySpec skey=new SecretKeySpec(key, "HmacMD5"); // mac=Mac.getInstance("HmacMD5"); // mac.init(skey); //mentalis_mac = new Org.Mentalis.Security.Cryptography.HMAC(new System.Security.Cryptography.MD5CryptoServiceProvider(), key); mentalis_mac = new System.Security.Cryptography.HMACMD5(key); cs = new System.Security.Cryptography.CryptoStream(System.IO.Stream.Null, mentalis_mac, System.Security.Cryptography.CryptoStreamMode.Write); }
public void init(byte[] key) { if(key.Length>bsize) { byte[] tmp=new byte[bsize]; Array.Copy(key, 0, tmp, 0, bsize); key=tmp; } // SecretKeySpec skey=new SecretKeySpec(key, "HmacSHA1"); // mac=Mac.getInstance("HmacSHA1"); // mac.init(skey); //mentalis_mac = new Org.Mentalis.Security.Cryptography.HMAC(new System.Security.Cryptography.SHA1CryptoServiceProvider(), key); mentalis_mac = new System.Security.Cryptography.HMACSHA1(key); cs = new System.Security.Cryptography.CryptoStream( System.IO.Stream.Null, mentalis_mac, System.Security.Cryptography.CryptoStreamMode.Write); }
// static string thanks = "http://stackoverflow.com/questions/6999648/signing-post-form-in-c-sharp-for-uploading-to-amazon-s3"; public static string BuildURL(string AccessKey, string SecretKey, DateTime timeToExpire, string BucketName, string FileKey) { System.Security.Cryptography.HMAC hmacProvider = System.Security.Cryptography.HMAC.Create(); string returnString = string.Empty; hmacProvider.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(SecretKey); string expirationString = ConvertToUnixTimestamp(timeToExpire).ToString(); //System.Uri.UriSchemeHttp &/ System.Web.HttpUtility.UrlEncode string assembledRequest = "GET" + "\n" + "\n" + "\n" + expirationString + "\n" + "/" + BucketName + "/" + UrlEncode(FileKey); byte[] hashedSignature = hmacProvider.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(assembledRequest)); returnString = Convert.ToBase64String(hashedSignature); return("https://" + "s3.amazonaws.com/" + BucketName + "/" + FileKey + "?AWSAccessKeyId=" + AccessKey + "&Expires=" + expirationString + "&Signature=" + UrlEncode(returnString)); }
public void init(byte[] key) { int bsize = getBlockSize(); if (key.Length > bsize) { byte[] tmp = new byte[bsize]; Array.Copy(key, 0, tmp, 0, bsize); key = tmp; } // SecretKeySpec skey=new SecretKeySpec(key, "HmacMD5"); // mac=Mac.getInstance("HmacMD5"); // mac.init(skey); hmac = CreateHMAC(key); cs = new System.Security.Cryptography.CryptoStream(System.IO.Stream.Null, hmac, System.Security.Cryptography.CryptoStreamMode.Write); }
public HMACBuildInAdapter(System.Security.Cryptography.HMAC a_hmac, int a_block_size) : base(a_hmac.HashSize / 8, a_block_size) { m_hmac = a_hmac; m_key = new byte[0]; }
private string calckey(String baseKey, long ts) { ts /= 30; int base32len = baseKey.Length; int secretlen = (base32len * 5 + 7) / 8; byte[] deckey = MhanoHarkness.Base32Url.FromBase32String(textBox1.Text); for (int i = secretlen; i < deckey.Length; i++) { deckey[i] = 0; } long chlg = ts; byte[] challenge; challenge = new byte[8]; for (int j = 7; j >= 0; j--) { challenge[j] = (byte)((int)chlg & 0xff); chlg >>= 8; } // hmac_sha1(secret, secretLen, challenge, 8, hash, SHA1_DIGEST_LENGTH); //Compute sha1 here /* * byte[] tmpkey; * tmpkey = new byte[64]; * //Create a 64 byte key, by xoring 0x36 with the actual key... * for (int i = 0; i < secretlen; i++) * { * tmpkey[i] = (byte)((int)deckey[i] ^ 0x36); * } * //...and initializing the rest to 0^0x36 which is actually 0x36. * for (int i = secretlen; i < tmpkey.Length; i++) * { * tmpkey[i] = 0x36; * } * * System.Security.Cryptography.HMAC sha1provider = System.Security.Cryptography.HMACSHA1.Create(); * sha1provider.Key = tmpkey; * byte[] shainner = sha1provider.ComputeHash(challenge); * * //Compute outer digest by padding the key with 0x5c * for (int i = 0; i < secretlen; i++) * { * tmpkey[i] = (byte)((int)deckey[i] ^ 0x5c); * } * for (int i = secretlen; i < tmpkey.Length; i++) * { * tmpkey[i] = 0x5c; * } * * sha1provider.Key = tmpkey; * byte[] result = sha1provider.ComputeHash(shainner); */ System.Security.Cryptography.HMAC sha1prov = System.Security.Cryptography.HMACSHA1.Create(); sha1prov.Key = deckey; byte[] result = sha1prov.ComputeHash(challenge); int offset = result[result.Length - 1] & 0xf; int truncatedHash = 0; for (int j = 0; j < 4; j++) { truncatedHash <<= 8; truncatedHash |= result[offset + j]; } truncatedHash &= 0x7FFFFFFF; truncatedHash %= 1000000; //System.Security.Cryptography.SHA1 string ret = truncatedHash.ToString(); if (ret.Length < 6) { ret.PadLeft(6, '0'); } return(ret); }
public HMACBuildInAdapter(System.Security.Cryptography.HMAC a_hmac, int a_block_size) : base(a_hmac.HashSize / 8, a_block_size) { m_hmac = a_hmac; m_key = new byte[0]; }
public AxCryptHMACSHA1Wrapper() { _hmac = Axantum.AxCrypt.Mono.Cryptography.AxCryptHMACSHA1.Create(); }