private void GetSymmetricKey() { var infoToWrite = GetInfoReady(this.merchantIdentifierField); IDigest digest = new Sha256Digest(); this.symmetricKey = new byte[digest.GetDigestSize()]; digest.Update((byte)(1 >> 24)); digest.Update((byte)(1 >> 16)); digest.Update((byte)(1 >> 8)); digest.Update((byte)1); digest.BlockUpdate(this.sharedSecret, 0, this.sharedSecret.Length); digest.BlockUpdate(infoToWrite, 0, infoToWrite.Length); digest.DoFinal(this.symmetricKey, 0); }
private BigInteger SMPHash(byte hash_number, BigInteger big_int_a, BigInteger big_int_b) { byte[] _encoded_mpi = null; Sha256Digest _sha256 = new Sha256Digest(); _sha256.Update(hash_number); Utility.EncodeMpiBytes(big_int_a, ref _encoded_mpi); _sha256.BlockUpdate(_encoded_mpi, 0, _encoded_mpi.Length); if (big_int_b != null) { Utility.EncodeMpiBytes(big_int_b, ref _encoded_mpi); _sha256.BlockUpdate(_encoded_mpi, 0, _encoded_mpi.Length); } byte[] _hashed_bytes = new byte[_sha256.GetDigestSize()]; _sha256.DoFinal(_hashed_bytes, 0); return(new BigInteger(1, _hashed_bytes)); }
private void BuildFromSeed(byte[] seed) { if (seed.Length != M) { throw new CoseException("Incorrect seed length"); } int keyCount = 1 << H; int p = Constants.Values[(int)_lmotsType].p; _x = new byte[keyCount][]; // Build LMS Private Key for (int q = 0; q < keyCount; q++) { byte[] qBytes = u32str(q); // Build LM-OTS private key for each leaf _x[q] = new byte[M * p]; for (UInt16 j = 0; j < p; j++) { Sha256Digest h = new Sha256Digest(); h.BlockUpdate(Identifier, 0, Identifier.Length); h.BlockUpdate(qBytes, 0, qBytes.Length); h.BlockUpdate(u16str(j), 0, 2); h.Update(0xff); h.BlockUpdate(seed, 0, seed.Length); h.DoFinal(_x[q], j * M); } } // Derive the public key ComputePublicKey(); }
/// <summary> /// From a single given key create a hashname with the intermediate hashes of other keys /// </summary> /// <returns>The key.</returns> /// <param name="csid">The identifier for the cipher set as a string.</param> /// <param name="keyData">The key data for the given csid.</param> /// <param name="intermediates">Intermediates.</param> public static string FromKey(string csid, byte[] keyData, IDictionary <string, string> intermediates) { Sha256Digest digest = new Sha256Digest(); List <string> keys = new List <string>(intermediates.Keys); keys.Add(csid); keys.Sort(); int digestSize = digest.GetDigestSize(); byte[] outhash = null; foreach (var key in keys) { if (outhash != null) { digest.BlockUpdate(outhash, 0, digestSize); } else { outhash = new byte[digestSize]; } byte inByte; try { inByte = Convert.ToByte(key, 16); } catch (FormatException) { return(null); } catch (OverflowException) { return(null); } catch (ArgumentException) { return(null); } digest.Update(inByte); digest.DoFinal(outhash, 0); digest.Reset(); digest.BlockUpdate(outhash, 0, digestSize); if (key == csid) { Sha256Digest keyDigest = new Sha256Digest(); keyDigest.BlockUpdate(keyData, 0, keyData.Length); keyDigest.DoFinal(outhash, 0); digest.BlockUpdate(outhash, 0, outhash.Length); } else { byte[] keyIntermediate = Base32Encoder.Decode(intermediates [key]); digest.BlockUpdate(keyIntermediate, 0, keyIntermediate.Length); } digest.DoFinal(outhash, 0); } return(Base32Encoder.Encode(outhash).TrimEnd(trimChars).ToLower()); }
public static string FromKeys(IDictionary <string, string> publicKeys) { // You've gotta have some keys to hash! if (publicKeys.Count <= 0) { return(null); } Sha256Digest digest = new Sha256Digest(); List <string> keys = new List <string>(publicKeys.Keys); keys.Sort(); int digestSize = digest.GetDigestSize(); byte[] outhash = null; foreach (var key in keys) { if (outhash != null) { digest.BlockUpdate(outhash, 0, digestSize); } else { outhash = new byte[digestSize]; } byte inByte; try { inByte = Convert.ToByte(key, 16); } catch (FormatException) { return(null); } catch (OverflowException) { return(null); } catch (ArgumentException) { return(null); } digest.Update(inByte); digest.DoFinal(outhash, 0); digest.Reset(); digest.BlockUpdate(outhash, 0, digestSize); byte[] keyData = Base32Encoder.Decode(publicKeys [key]); Sha256Digest keyDigest = new Sha256Digest(); keyDigest.BlockUpdate(keyData, 0, keyData.Length); keyDigest.DoFinal(outhash, 0); digest.BlockUpdate(outhash, 0, outhash.Length); digest.DoFinal(outhash, 0); } return(Base32Encoder.Encode(outhash).TrimEnd(trimChars).ToLower()); }