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 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 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); }
// 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)); }
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 override byte[] ComputeHash(byte[] buffer) { return(_hmac.ComputeHash(buffer)); }