/// <summary>
 ///     Calculate content hash of content in a stream.
 /// </summary>
 public static async Task <ContentHash> CalculateHashAsync(this StreamWithLength stream, HashType hashType)
 {
     using (var hasher = HashInfoLookup.Find(hashType).CreateContentHasher())
     {
         return(await hasher.GetContentHashAsync(stream));
     }
 }
Exemple #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);
        }
 /// <summary>
 ///     Calculate content hash of content in a byte array.
 /// </summary>
 public static ContentHash CalculateHash(this byte[] content, HashType hashType)
 {
     using (var hasher = HashInfoLookup.Find(hashType).CreateContentHasher())
     {
         return(hasher.GetContentHash(content));
     }
 }
Exemple #4
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);
        }
Exemple #5
0
        /// <summary>
        ///     Create a random value.
        /// </summary>
        public static ContentHash Random(HashType hashType = HashType.Vso0)
        {
            var hashInfo = HashInfoLookup.Find(hashType);
            var bytes    = ThreadSafeRandom.GetBytes(hashInfo.ByteLength);

            if (hashInfo is TaggedHashInfo taggedHashInfo)
            {
                bytes[bytes.Length - 1] = taggedHashInfo.AlgorithmId;
            }

            return(new ContentHash(hashType, bytes));
        }
        /// <summary>
        ///     Calculate content hash of content in a stream.
        /// </summary>
        public static Task <ContentHash> CalculateHashAsync(this StreamWithLength stream, HashType hashType)
        {
#if NET_COREAPP
            if (stream.Stream is FileStream fileStream)
            {
                return(Task.FromResult(fileStream.HashFile(hashType)));
            }
#endif // NET_COREAPP

            var hasher = HashInfoLookup.GetContentHasher(hashType);
            return(hasher.GetContentHashAsync(stream));
        }
Exemple #7
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);
        }
 /// <summary>
 /// Whether <paramref name="contentHash"/> is the empty hash.
 /// </summary>
 public static bool IsEmptyHash(this ContentHash contentHash)
 {
     return(contentHash == HashInfoLookup.Find(contentHash.HashType).EmptyHash);
 }
 public static bool IsZero(this ContentHash contentHash)
 {
     return(contentHash == HashInfoLookup.Find(contentHash.HashType).Zero);
 }
        /// <summary>
        ///     Calculate content hash of content in a stream.
        /// </summary>
        public static Task <ContentHash> CalculateHashAsync(this StreamWithLength stream, HashType hashType)
        {
            var hasher = HashInfoLookup.GetContentHasher(hashType);

            return(hasher.GetContentHashAsync(stream));
        }
 /// <summary>
 ///     Calculate content hash of content in a byte array.
 /// </summary>
 public static ContentHash CalculateHash(this byte[] content, HashType hashType)
 {
     return(HashInfoLookup.GetContentHasher(hashType).GetContentHash(content));
 }