示例#1
0
        /// <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);
        }
示例#2
0
        /// <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);
        }
示例#3
0
        /// <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);
        }
示例#4
0
 /// <nodoc />
 internal ShortReadOnlyFixedBytes(ref ReadOnlyFixedBytes fixedBytes)
 {
     fixed(byte *d = &_bytes.FixedElementField)
     {
         for (int i = 0; i < MaxLength; i++)
         {
             d[i] = fixedBytes[i];
         }
     }
 }
示例#5
0
        /// <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);
        }
示例#6
0
        /// <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);
        }
示例#7
0
 /// <nodoc />
 public ShortHash(ReadOnlyFixedBytes bytes) => Value = new ShortReadOnlyFixedBytes(ref bytes);
示例#8
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="ContentHash" /> struct from bytes
 /// </summary>
 private ContentHash(HashType hashType, ReadOnlyFixedBytes bytes)
 {
     _hashType = hashType;
     _bytes    = bytes;
 }
示例#9
0
 /// <nodoc />
 public static ContentHash FromFixedBytes(HashType hashType, ReadOnlyFixedBytes bytes)
 {
     return(new ContentHash(hashType, bytes));
 }