/// <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(); }
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); }