public void WeakFingerprintHashToMemoization() { WeakFingerprintHash weak = RandomHelpers.CreateRandomWeakFingerprintHash(); Fingerprint fingerprint = weak.ToMemoization(); Assert.Equal(weak.ToArray(), fingerprint.ToByteArray()); }
public void ToByteArray() { var bytes = Enumerable.Range(0, 7).Select(i => (byte)i).ToArray(); var fingerprint = new Fingerprint(bytes); var exported = fingerprint.ToByteArray(); Assert.Equal(bytes, exported); }
/// <nodoc /> public static Xldb.Fingerprint ToFingerprint(this Fingerprint fingerprint) { return(new Xldb.Fingerprint() { Length = fingerprint.Length, Bytes = Google.Protobuf.ByteString.CopyFrom(fingerprint.ToByteArray()) }); }
/// <summary> /// Adds a content hash to the fingerprint stream without adding a type prefix. /// </summary> protected void AddInnerHash(Fingerprint fingerprint) { var bytes = fingerprint.ToByteArray(); Contract.Assert(bytes.Length == ContentHashingUtilities.HashInfo.ByteLength); for (int i = 0; i < ContentHashingUtilities.HashInfo.ByteLength; i++) { AddInnerByte(bytes[i]); } }
/// <summary> /// Adds a fingerprint to the fingerprint stream. /// </summary> protected void AddInnerFingerprint(Fingerprint fingerprint) { var bytes = fingerprint.ToByteArray(); Contract.Assert(bytes.Length == FingerprintUtilities.FingerprintLength); for (int i = 0; i < bytes.Length; i++) { AddInnerByte(bytes[i]); } }
/// <summary> /// Return an array of bytes that is the hash /// </summary> /// <returns> /// Array of bytes that represents the hash /// </returns> public byte[] ToArray() { return(RawHash.ToByteArray()); }
/// <summary> /// Returns a generic <see cref="ContentFingerprint"/> (not distinguished as strong or weak). /// TODO: The generic fingerprint type can go away as soon as we *only* do two-phase (weak -> strong) lookups; /// as of writing we are transitioning. /// </summary> public ContentFingerprint ToGenericFingerprint() { return(new ContentFingerprint(FingerprintUtilities.CreateFrom(Hash.ToByteArray()))); }
public byte[] Encode(IStunContext ctx) { bool useMessageIntegrity = !string.IsNullOrEmpty(ctx?.Username) && !string.IsNullOrEmpty(ctx?.Password) && !string.IsNullOrEmpty(ctx?.Realm); var c = (ushort)Class; var m = (ushort)Method; int type = (m & 0x0f80) << 2 | (m & 0x0070) << 1 | (m & 0x000f) << 0 | (c & 0x2) << 7 | (c & 0x1) << 4; using (var ms = new MemoryStream()) { List <Attribute> attrs = Attributes.ToList(); if (!string.IsNullOrEmpty(ctx?.Username)) { attrs.Add(new Username(ctx.Username)); } if (ctx?.Nonce != null) { attrs.Add(new Attributes.Nonce(ctx.Nonce)); } if (!string.IsNullOrEmpty(ctx?.Realm)) { attrs.Add(new Realm(ctx.Realm)); } byte[] encodedAttrs; using (var ams = new MemoryStream()) { foreach (Attribute attr in attrs) { byte[] asBytes = attr.ToByteArray(TransactionId); ams.Write(asBytes, 0, asBytes.Length); } encodedAttrs = ams.ToArray(); } // 8 bytes for Fingerprint var messageLength = (ushort)(encodedAttrs.Length + FingerprintBytes); if (useMessageIntegrity) { messageLength += MessageIntegrityBytes; } ms.Write(((ushort)type).ToBytes(), 0, 2); ms.Write(messageLength.ToBytes(), 0, 2); ms.Write(MagicCookie, 0, MagicCookie.Length); ms.Write(TransactionId, 0, TransactionId.Length); ms.Write(encodedAttrs, 0, encodedAttrs.Length); if (useMessageIntegrity) { var lengthWithoutFingerprint = (ushort)(messageLength - FingerprintBytes); byte[] toCalc = ms.ToArray(); lengthWithoutFingerprint.ToBytes().CopyTo(toCalc, 2); MessageIntegrity mi = MessageIntegrity.Calculate( ctx?.Username, ctx?.Password, ctx?.Realm, toCalc); ms.Write(mi.ToByteArray(), 0, MessageIntegrityBytes); } Fingerprint fingerprint = Fingerprint.FromMessage( ms.ToArray() ); ms.Write(fingerprint.ToByteArray(), 0, FingerprintBytes); return(ms.ToArray()); } }