/// <summary> /// Initializes a new instance of the <see cref="ContentHash" /> struct from binary reader /// </summary> public ContentHash(BinaryReader reader) { Contract.Requires(reader != null); _hashType = (HashType)reader.ReadByte(); _bytes = ReadOnlyFixedBytes.ReadFrom(reader); }
/// <summary> /// Attempt to create from a known type and string (without type). /// </summary> public static bool TryParse(HashType hashType, string serialized, out ContentHash contentHash) { Contract.Requires(serialized != null); var hashInfo = HashInfoLookup.Find(hashType); if (serialized.Length != hashInfo.StringLength) { contentHash = default(ContentHash); return(false); } if (!ReadOnlyFixedBytes.TryParse(serialized, out var bytes, out _)) { contentHash = default(ContentHash); return(false); } contentHash = new ContentHash(hashType, bytes); if (HashInfoLookup.Find(hashType) is TaggedHashInfo && !AlgorithmIdHelpers.IsHashTagValid(contentHash)) { contentHash = default(ContentHash); return(false); } return(true); }
/// <summary> /// Initializes a new instance of the <see cref="ContentHash" /> struct from binary reader /// </summary> public ContentHash(HashType hashType, BinaryReader reader) : this() { Contract.Requires(hashType != HashType.Unknown); Contract.Requires(reader != null); _hashType = hashType; _bytes = ReadOnlyFixedBytes.ReadFrom(reader, ByteLength); }
/// <nodoc /> internal ShortReadOnlyFixedBytes(ref ReadOnlyFixedBytes fixedBytes) { fixed(byte *d = &_bytes.FixedElementField) { for (int i = 0; i < MaxLength; i++) { d[i] = fixedBytes[i]; } } }
/// <summary> /// Initializes a new instance of the <see cref="ContentHash" /> struct from byte array /// </summary> public ContentHash(byte[] buffer, int offset = 0, SerializeHashBytesMethod serializeMethod = SerializeHashBytesMethod.Trimmed) { Contract.Requires(buffer != null); _hashType = (HashType)buffer[offset++]; var length = serializeMethod == SerializeHashBytesMethod.Trimmed ? HashInfoLookup.Find(_hashType).ByteLength : MaxHashByteLength; _bytes = new ReadOnlyFixedBytes(buffer, length, offset); }
/// <summary> /// Initializes a new instance of the <see cref="ContentHash" /> struct from byte array /// </summary> public ContentHash(HashType hashType, ReadOnlySpan <byte> buffer, int offset = 0) { Contract.Requires(hashType != HashType.Unknown); int hashBytesLength = HashInfoLookup.Find(hashType).ByteLength; if (buffer.Length < (hashBytesLength + offset)) { throw new ArgumentException($"Buffer undersized length=[{buffer.Length}] for hash type=[{hashType}]"); } _hashType = hashType; _bytes = new ReadOnlyFixedBytes(buffer, hashBytesLength, offset); }
/// <nodoc /> public ShortHash(ReadOnlyFixedBytes bytes) => Value = new ShortReadOnlyFixedBytes(ref bytes);
/// <summary> /// Initializes a new instance of the <see cref="ContentHash" /> struct from bytes /// </summary> private ContentHash(HashType hashType, ReadOnlyFixedBytes bytes) { _hashType = hashType; _bytes = bytes; }
/// <nodoc /> public static ContentHash FromFixedBytes(HashType hashType, ReadOnlyFixedBytes bytes) { return(new ContentHash(hashType, bytes)); }