/** * Performs the extract part of the key derivation function. * * @param salt the salt to use * @param ikm the input keying material * @return the PRK as KeyParameter */ private KeyParameter Extract(byte[] salt, byte[] ikm) { _hMacHash.Init(new KeyParameter(ikm)); if (salt == null) { // TODO check if hashLen is indeed same as HMAC size _hMacHash.Init(new KeyParameter(new byte[_hashLen])); } else { _hMacHash.Init(new KeyParameter(salt)); } _hMacHash.BlockUpdate(ikm, 0, ikm.Length); byte[] prk = new byte[_hashLen]; _hMacHash.DoFinal(prk, 0); return(new KeyParameter(prk)); }
private byte[] ComputeClientSignature(byte[] authMessage) { byte[] result = new byte[m_clientSignature.GetMacSize()]; lock (m_clientSignature) { m_clientSignature.BlockUpdate(authMessage, 0, authMessage.Length); m_clientSignature.DoFinal(result, 0); } return(result); }
public static string CalculateRequestHash(HMac mac, byte[] data) { mac.Init(new KeyParameter(Encoding.UTF8.GetBytes(PagarMeService.DefaultApiKey))); mac.BlockUpdate(data, 0, data.Length); byte[] result = new byte[mac.GetMacSize()]; mac.DoFinal(result, 0); string hex = BitConverter.ToString(result).Replace("-", "").ToLower(); return(hex); }
public static string GenerateHMACBC(byte[] data, byte[] EK) { var hmac = new HMac(new Sha256Digest()); hmac.Init(new KeyParameter(EK)); byte[] hashMessage = new byte[hmac.GetMacSize()]; hmac.BlockUpdate(data, 0, data.Length); hmac.DoFinal(hashMessage, 0); return(Convert.ToBase64String(hashMessage)); }
/// <summary> /// HMAC-SHA256 hash /// </summary> public static byte[] HmacSha256(byte[] data, byte[] key) { var hmac = new HMac(new Sha256Digest()); hmac.Init(new KeyParameter(key)); byte[] hash = new byte[hmac.GetMacSize()]; hmac.BlockUpdate(data, 0, data.Length); hmac.DoFinal(hash, 0); return(hash); }
/// <summary> /// Calculate the current code for the authenticator. /// </summary> /// <param name="resyncTime">flag to resync time</param> /// <returns>authenticator code</returns> protected override string CalculateCode(bool resyncTime = false, long interval = -1) { // sync time if required if (resyncTime || ServerTimeDiff == 0) { if (interval > 0) { ServerTimeDiff = interval * Period * 1000L - CurrentTime; } else { Sync(); } } var hmac = new HMac(new Sha1Digest()); hmac.Init(new KeyParameter(SecretKey)); var codeIntervalArray = BitConverter.GetBytes(CodeInterval); if (BitConverter.IsLittleEndian) { Array.Reverse(codeIntervalArray); } hmac.BlockUpdate(codeIntervalArray, 0, codeIntervalArray.Length); var mac = new byte[hmac.GetMacSize()]; hmac.DoFinal(mac, 0); // the last 4 bits of the mac say where the code starts (e.g. if last 4 bit are 1100, we start at byte 12) var start = mac[19] & 0x0f; // extract those 4 bytes var bytes = new byte[4]; Array.Copy(mac, start, bytes, 0, 4); if (BitConverter.IsLittleEndian) { Array.Reverse(bytes); } var fullcode = BitConverter.ToUInt32(bytes, 0) & 0x7fffffff; // build the alphanumeric code var code = new StringBuilder(); for (var i = 0; i < CODE_DIGITS; i++) { code.Append(STEAMCHARS[fullcode % STEAMCHARS.Length]); fullcode /= (uint)STEAMCHARS.Length; } return(code.ToString()); }
byte[] ComputeServerSignature(byte[] authMessage) { var result = new byte[m_serverSignature.GetMacSize()]; lock (m_serverSignature) { m_serverSignature.BlockUpdate(authMessage, 0, authMessage.Length); m_serverSignature.DoFinal(result, 0); } return(result); }
public static byte[] Compute(IDigest hash, byte[] key, byte[] data, int position, int length) { data.ValidateParameters(position, length); HMac hmac = new HMac(hash); hmac.Init(new KeyParameter(key)); byte[] result = new byte[hmac.GetMacSize()]; hmac.BlockUpdate(data, position, length); hmac.DoFinal(result, 0); return(result); }
/// <summary> /// This method computes and returns the hash in bytes /// </summary> /// <param name="value"></param> /// <returns></returns> public byte[] ComputeHash(byte[] value) { if (value == null) { throw new ArgumentNullException("value"); } byte[] resBuf = new byte[_hmac.GetMacSize()]; _hmac.BlockUpdate(value, 0, value.Length); _hmac.DoFinal(resBuf, 0); return(resBuf); }
public byte[] HMAC(byte[] data, byte[] key) { var hmac = new HMac(new Sha256Digest()); hmac.Init(new KeyParameter(key)); byte[] result = new byte[hmac.GetMacSize()]; hmac.BlockUpdate(data, 0, data.Length); hmac.DoFinal(result, 0); return(result); }
//Methods #region ComputeHash public Byte[] ComputeHash(Byte[] bytes) { var hmac = new HMac(new Sha256Digest()); hmac.Init(new KeyParameter(key)); Byte[] result = new Byte[hmac.GetMacSize()]; hmac.BlockUpdate(bytes, 0, bytes.Length); hmac.DoFinal(result, 0); return(result); }
/// <summary> /// Calculate the current code for the authenticator. /// Trion's implementation is broken in that they don't built the signed integer correctly from the 4-byte array, so we have to override /// the proper method /// </summary> /// <param name="resyncTime">flag to resync time</param> /// <returns>authenticator code</returns> protected override string CalculateCode(bool resyncTime = false, long interval = -1) { // sync time if required if (resyncTime == true || ServerTimeDiff == 0) { if (interval > 0) { ServerTimeDiff = (interval * 30000L) - CurrentTime; } else { Sync(); } } HMac hmac = new HMac(new Sha1Digest()); hmac.Init(new KeyParameter(SecretKey)); byte[] codeIntervalArray = BitConverter.GetBytes(CodeInterval); if (BitConverter.IsLittleEndian) { Array.Reverse(codeIntervalArray); } hmac.BlockUpdate(codeIntervalArray, 0, codeIntervalArray.Length); byte[] mac = new byte[hmac.GetMacSize()]; hmac.DoFinal(mac, 0); // the last 4 bits of the mac say where the code starts (e.g. if last 4 bit are 1100, we start at byte 12) int start = mac[19] & 0x0f; // extract those 4 bytes byte[] bytes = new byte[4]; Array.Copy(mac, start, bytes, 0, 4); if (BitConverter.IsLittleEndian) { Array.Reverse(bytes); } // this is where Trion is broken and their version uses all 32bits //uint fullcode = BitConverter.ToUInt32(bytes, 0) & 0x7fffffff; uint fullcode = BitConverter.ToUInt32(bytes, 0); // we use the last 8 digits of this code in radix 10 uint codemask = (uint)Math.Pow(10, CodeDigits); string format = new string('0', CodeDigits); string code = (fullcode % codemask).ToString(format); // New glyph authenticator now uses 6, but takes the first 6 of 8 rather the proper last 6, so again we override the standard implementation code = code.Substring(0, 6); return(code); }
//void ICryptoLibrary.ProcessAesGcmBlocks(bool encryptOrDecrypt, byte[] key, byte[] iv, byte[] input, byte[] output) //{ // if (input.Length != output.Length) throw new ArgumentException(); // var gcmBlockCipher = new GcmBlockCipher(new AesEngine()); // var blockSize = CryptoLibraries.AesBlockSize; // if (input.Length % blockSize != 0) throw new ArgumentException(); // gcmBlockCipher.Init(encryptOrDecrypt, new ParametersWithIV(new KeyParameter(key), iv)); // var numberOfBlocks = input.Length / blockSize; // int offset = 0; // for (int i = 0; i < numberOfBlocks; i++) // { // gcmBlockCipher.ProcessAadBytes(); // gcmBlockCipher.ProcessBytes(); // gcmBlockCipher.GetMac(); // // ProcessBlock(input, offset, output, offset); // offset += blockSize; // } //} byte[] ICryptoLibrary.GetSha256HMAC(byte[] key, byte[] data) { var hmac = new HMac(new Sha256Digest()); hmac.Init(new KeyParameter(key)); var result = new byte[hmac.GetMacSize()]; hmac.BlockUpdate(data, 0, data.Length); hmac.DoFinal(result, 0); return(result); }
public void Init(BigInteger n, BigInteger d, byte[] message) { this.n = n; Arrays.Fill(V, (byte)0x01); Arrays.Fill(K, (byte)0); int size = BigIntegers.GetUnsignedByteLength(n); byte[] x = new byte[size]; byte[] dVal = BigIntegers.AsUnsignedByteArray(d); Array.Copy(dVal, 0, x, x.Length - dVal.Length, dVal.Length); byte[] m = new byte[size]; BigInteger mInt = BitsToInt(message); if (mInt.CompareTo(n) >= 0) { mInt = mInt.Subtract(n); } byte[] mVal = BigIntegers.AsUnsignedByteArray(mInt); Array.Copy(mVal, 0, m, m.Length - mVal.Length, mVal.Length); hMac.Init(new KeyParameter(K)); hMac.BlockUpdate(V, 0, V.Length); hMac.Update((byte)0x00); hMac.BlockUpdate(x, 0, x.Length); hMac.BlockUpdate(m, 0, m.Length); hMac.DoFinal(K, 0); hMac.Init(new KeyParameter(K)); hMac.BlockUpdate(V, 0, V.Length); hMac.DoFinal(V, 0); hMac.BlockUpdate(V, 0, V.Length); hMac.Update((byte)0x01); hMac.BlockUpdate(x, 0, x.Length); hMac.BlockUpdate(m, 0, m.Length); hMac.DoFinal(K, 0); hMac.Init(new KeyParameter(K)); hMac.BlockUpdate(V, 0, V.Length); hMac.DoFinal(V, 0); }
public static string Hashfile(byte[] bytes, string key) { var hmac = new HMac(new Sha256Digest()); hmac.Init(new KeyParameter(Encoding.UTF8.GetBytes(key))); byte[] result = new byte[hmac.GetMacSize()]; hmac.BlockUpdate(bytes, 0, bytes.Length); hmac.DoFinal(result, 0); return(GetStringFromHash(result)); }
private static void hmac_hash(IDigest digest, byte[] secret, byte[] seed, byte[] output) { HMac mac = new HMac(digest); mac.Init(new KeyParameter(secret)); byte[] a = seed; int size = digest.GetDigestSize(); int iterations = (output.Length + size - 1) / size; byte[] buf = new byte[mac.GetMacSize()]; byte[] buf2 = new byte[mac.GetMacSize()]; for (int i = 0; i < iterations; i++) { mac.BlockUpdate(a, 0, a.Length); mac.DoFinal(buf, 0); a = buf; mac.BlockUpdate(a, 0, a.Length); mac.BlockUpdate(seed, 0, seed.Length); mac.DoFinal(buf2, 0); Array.Copy(buf2, 0, output, (size * i), System.Math.Min(size, output.Length - (size * i))); } }
internal static byte[] HmacSha512Digest(byte[] input, int offset, int length, byte[] hmacKey) { var output = new byte[64]; HMac hmacsha512Obj = new HMac(new Sha512Digest()); var param = new Org.BouncyCastle.Crypto.Parameters.KeyParameter(hmacKey); hmacsha512Obj.Init(param); hmacsha512Obj.BlockUpdate(input, offset, length); hmacsha512Obj.DoFinal(output, 0); return(output); }
private byte[] ComputeHMacSHA256(string data, byte[] key) { var hmacSha256Algorism = new HMac(new Sha256Digest()); hmacSha256Algorism.Init(new KeyParameter(key)); var dataUtf8 = Encoding.UTF8.GetBytes(data); hmacSha256Algorism.BlockUpdate(dataUtf8, 0, dataUtf8.Length); var result = new byte[hmacSha256Algorism.GetUnderlyingDigest().GetDigestSize()]; hmacSha256Algorism.DoFinal(result, 0); return(result); }
public byte[] Hash(string text, string key) { var hmac = new HMac(new Sha256Digest()); hmac.Init(new KeyParameter(Encoding.UTF8.GetBytes(key))); byte[] result = new byte[hmac.GetMacSize()]; byte[] bytes = Encoding.UTF8.GetBytes(text); hmac.BlockUpdate(bytes, 0, bytes.Length); hmac.DoFinal(result, 0); return(result); }
public static string HmacSHA256(string s1, string key) { var hmac = new HMac(new Sha256Digest()); hmac.Init(new KeyParameter(Encoding.UTF8.GetBytes(key))); byte[] result = new byte[hmac.GetMacSize()]; byte[] bytes = Encoding.UTF8.GetBytes(s1); hmac.BlockUpdate(bytes, 0, bytes.Length); hmac.DoFinal(result, 0); return(Base64.ToBase64String(result)); }
public byte[] ComputeHash(byte[] value, int offset, int length) { if (value == null) { throw new ArgumentNullException(nameof(value)); } byte[] resBuf = new byte[Hmac.GetMacSize()]; Hmac.BlockUpdate(value, offset, length); Hmac.DoFinal(resBuf, offset); return(resBuf); }
public string LlamadaApi(Uri endpoint, Token tokenAcceso, string payload, bool logIt) { string issued = EpochBasedTime(0).ToString(); string nonce = Nonce(12); string token = payload + nonce + issued; var hmac = new HMac(new Sha512Digest()); hmac.Init(new KeyParameter(Encoding.UTF8.GetBytes(ClientSecret))); byte[] result = new byte[hmac.GetMacSize()]; byte[] bytes = Encoding.UTF8.GetBytes(token); hmac.BlockUpdate(bytes, 0, bytes.Length); hmac.DoFinal(result, 0); var request = new RestRequest(Method.POST); var client = new RestClient(endpoint); request.AddHeader("Cache-Control", "no-cache"); request.AddHeader("Content-Type", "application/json"); request.AddHeader("X-Banorte-Hmac-Issued", issued); request.AddHeader("X-Banorte-Hmac-Nonce", nonce); request.AddHeader("Authorization", "Bearer " + tokenAcceso.AccessToken); request.AddHeader("X-Banorte-Hmac-Token", Convert.ToBase64String(result)); request.AddParameter("undefined", payload, ParameterType.RequestBody); var stopwatch = new Stopwatch(); stopwatch.Start(); IRestResponse response = client.Execute(request); stopwatch.Stop(); if (logIt) { LogRequest(request, response, client, stopwatch.ElapsedMilliseconds); } if (response.ResponseStatus == ResponseStatus.Error) { throw response.ErrorException; } else if (response.StatusCode != HttpStatusCode.OK) { throw new Exception(response.Content); } Debug.WriteLine(response.Content); return(response.Content); }
public static string Signature(string httpmethod, string url, string parameterString, string oauthToken) { string baseString = httpmethod.ToUpper() + "&" + PercentEncode(url) + "&" + PercentEncode(parameterString); string signingKey = PercentEncode(consumerSecret) + "&" + PercentEncode(oauthToken); var sha = new HMac(new Sha1Digest()); sha.Init(new KeyParameter(Encoding.UTF8.GetBytes(signingKey))); var data = Encoding.UTF8.GetBytes(baseString); sha.BlockUpdate(data, 0, data.Length); byte[] result = new byte[20]; sha.DoFinal(result, 0); return(Convert.ToBase64String(result)); }
private byte[] HMacSha256(byte[] inputBytes) { var sha256 = new Sha256Digest(); var hMac = new HMac(sha256); var keyParam = new KeyParameter(_config.DigestKey); hMac.Init(keyParam); hMac.BlockUpdate(inputBytes, 0, inputBytes.Length); var hash = new byte[hMac.GetMacSize()]; hMac.DoFinal(hash, 0); return(hash); }
/// <summary> /// 获取机器的唯一识别码 /// </summary> /// <param name="digest">计算识别码使用的HASH函数</param> /// <returns>唯一识别码</returns> public static byte[] getHash(IDigest digest) { HMac hmac = new HMac(digest); byte[] resBuf = new byte[hmac.GetMacSize()]; string m = ProcessorID() + BIOSSN() + BaseBoardSN(); string k = "TaraRansomeware" + OperatingSN(); hmac.Init(new KeyParameter(Encoding.UTF8.GetBytes(k))); hmac.BlockUpdate(Encoding.UTF8.GetBytes(m), 0, m.Length); hmac.DoFinal(resBuf, 0); return(resBuf); }
public Packet ChannelEncrypt(ICipherSetRemoteInfo channelInfo, Packet inner) { var ci = (CS1ARemoteInfo)channelInfo; // TODO: Validate we don't care about endianess of IV here // Setup and encrypt the actual data byte[] aesIV = new byte[16]; Buffer.BlockCopy(BitConverter.GetBytes(ci.IV), 0, aesIV, 0, 4); Array.Clear(aesIV, 4, 12); var cipher = new SicBlockCipher(new AesFastEngine()); var parameters = new ParametersWithIV(new KeyParameter(ci.EncryptionKey), aesIV); cipher.Init(true, parameters); var encryptedInner = new byte[inner.FullPacket.Length]; BufferedBlockCipher bufferCipher = new BufferedBlockCipher(cipher); var offset = bufferCipher.ProcessBytes(inner.FullPacket, encryptedInner, 0); bufferCipher.DoFinal(encryptedInner, offset); // Hmac the output byte[] hmacKey = new byte[20]; Buffer.BlockCopy(ci.EncryptionKey, 0, hmacKey, 0, 16); Buffer.BlockCopy(BitConverter.GetBytes(ci.IV), 0, hmacKey, 16, 4); var hmac = new HMac(new Sha256Digest()); hmac.Init(new KeyParameter(hmacKey)); hmac.BlockUpdate(encryptedInner, 0, encryptedInner.Length); byte[] mac = new byte[hmac.GetMacSize()]; hmac.DoFinal(mac, 0); var foldedMac = Helpers.Fold(mac, 3); // Create the outgoing packet Packet outPacket = new Packet(); outPacket.Body = new byte[encryptedInner.Length + 24]; Buffer.BlockCopy(ci.Token, 0, outPacket.Body, 0, 16); Buffer.BlockCopy(BitConverter.GetBytes(ci.IV), 0, outPacket.Body, 16, 4); Buffer.BlockCopy(encryptedInner, 0, outPacket.Body, 20, encryptedInner.Length); Buffer.BlockCopy(foldedMac, 0, outPacket.Body, outPacket.Body.Length - 4, 4); // Next IV next packet ++ci.IV; return(outPacket); }
/// <summary> /// Calculate the current code for the authenticator. /// </summary> /// <returns>authenticator code</returns> protected override string CalculateCode(bool sync = false, long counter = -1) { if (sync == true) { if (counter == -1) { throw new ArgumentException("counter must be >= 0"); } // set as previous because we increment Counter = counter - 1; } HMac hmac = new HMac(new Sha1Digest()); hmac.Init(new KeyParameter(SecretKey)); // increment counter Counter++; byte[] codeIntervalArray = BitConverter.GetBytes(Counter); if (BitConverter.IsLittleEndian) { Array.Reverse(codeIntervalArray); } hmac.BlockUpdate(codeIntervalArray, 0, codeIntervalArray.Length); byte[] mac = new byte[hmac.GetMacSize()]; hmac.DoFinal(mac, 0); // the last 4 bits of the mac say where the code starts (e.g. if last 4 bit are 1100, we start at byte 12) int start = mac[19] & 0x0f; // extract those 4 bytes byte[] bytes = new byte[4]; Array.Copy(mac, start, bytes, 0, 4); if (BitConverter.IsLittleEndian) { Array.Reverse(bytes); } uint fullcode = BitConverter.ToUInt32(bytes, 0) & 0x7fffffff; // we use the last 8 digits of this code in radix 10 uint codemask = (uint)Math.Pow(10, CodeDigits); string format = new string('0', CodeDigits); string code = (fullcode % codemask).ToString(format); return(code); }
/// <summary> /// HmacSha256 /// </summary> /// <param name="data"></param> /// <param name="key"></param> /// <returns></returns> private string HmacSha256(string data, string key) { var encData = Encoding.UTF8.GetBytes(data); var hmac = new HMac(new Sha256Digest()); hmac.Init(new KeyParameter(Encoding.UTF8.GetBytes(key))); var output = new byte[hmac.GetMacSize()]; hmac.BlockUpdate(encData, 0, encData.Length); hmac.DoFinal(output, 0); return(BitConverter.ToString(output).Replace("-", string.Empty)); }
/// <summary> /// Performs a SHA1 Hash /// </summary> /// <param name="key">Key</param> /// <param name="value">SHA value</param> /// <returns>Base 64 encoded string representing the hash</returns> public static string Sha1Hash( string key, string value) { var hmac = new HMac(new Sha1Digest()); hmac.Init(new KeyParameter(Encoding.UTF8.GetBytes(key))); var result = new byte[hmac.GetMacSize()]; var bytes = Encoding.UTF8.GetBytes(value); hmac.BlockUpdate(bytes, 0, bytes.Length); hmac.DoFinal(result, 0); return(Convert.ToBase64String(result)); }
private static void HashFunc(HMac hMac, byte[] unique, byte[] block, byte[] outBytes, int outOff) { byte[] state = new byte[hMac.GetMacSize()]; var cipherParam = new KeyParameter(unique); hMac.Init(cipherParam); hMac.BlockUpdate(_Salt, 0, _Salt.Length); hMac.BlockUpdate(block, 0, block.Length); hMac.DoFinal(state, 0); Array.Copy(state, 0, outBytes, outOff, state.Length); for (int iteration = 1; iteration < ITERATION_COUNT; iteration++) { hMac.Init(cipherParam); hMac.BlockUpdate(state, 0, state.Length); hMac.DoFinal(state, 0); for (int stateIndex = 0; stateIndex < state.Length; stateIndex++) { outBytes[outOff + stateIndex] ^= state[stateIndex]; } } }