/// <summary> /// Computes the hash. /// </summary> /// <param name="version">The version.</param> /// <param name="header">The header.</param> /// <param name="parameters">The parameters.</param> /// <param name="data">The scope bytes.</param> /// <param name="privacy">The privacy provider.</param> /// <param name="length">The length bytes.</param> /// <returns></returns> public OctetString ComputeHash(VersionCode version, ISegment header, SecurityParameters parameters, ISnmpData data, IPrivacyProvider privacy, byte[] length) { if (header == null) { throw new ArgumentNullException(nameof(header)); } if (parameters == null) { throw new ArgumentNullException(nameof(parameters)); } if (data == null) { throw new ArgumentNullException(nameof(data)); } if (privacy == null) { throw new ArgumentNullException(nameof(privacy)); } var key = PasswordToKey(_password, parameters.EngineId.GetRaw()); using (var sha384 = new HMACSHA384(key)) { var hash = sha384.ComputeHash(ByteTool.PackMessage(length, version, header, parameters, data).ToBytes()); #if NET471 sha384.Clear(); #endif var result = new byte[DigestLength]; Buffer.BlockCopy(hash, 0, result, 0, result.Length); return(new OctetString(result)); } }
public static string HMACSHA384(string sourceText, string key) { var keyByte = Encoding.UTF8.GetBytes(key); var sourceBytes = Encoding.UTF8.GetBytes(sourceText); using (var hmacSha384 = new HMACSHA384(keyByte)) { var bytes = hmacSha384.ComputeHash(sourceBytes); hmacSha384.Clear(); return(BitConverter.ToString(bytes).Replace("-", "").ToLower()); } }
public static string CreateMacCode(string target, string key) { byte[] data = Encoding.UTF8.GetBytes(target); byte[] keyData = Encoding.UTF8.GetBytes(key); // using (var hmac = new HMACSHA512(keyData)) using (var hmac = new HMACSHA384(keyData)) { var bs = hmac.ComputeHash(data); hmac.Clear(); return(Convert.ToBase64String(bs)); } }
} //compute hash from arguments and return hash value as string private static string GetHMACSHA384Hash(string text) { //create variables for computing hash and making it a string UnicodeEncoding UniCode = new UnicodeEncoding(); byte[] HashResult; byte[] msg = UniCode.GetBytes(text); HMACSHA384 hashString = new HMACSHA384(); string Str = ""; //compute hash with HMACSHA384 module and format output as string //convert bytes in HashResult to string values HashResult = hashString.ComputeHash(msg); foreach (byte x in HashResult) { Str += String.Format("{0:x2}", x); } //clear excess resource usage hashString.Clear(); return(Str); } //compute hash from arguments and return hash value as string