/// <summary> /// Computes the hash of a given InputStream. This is a wrapper over the HashAlgorithm crypto functions. /// </summary> /// <param name="stream">the source stream. Use a MemoryStream if uncertain.</param> /// <param name="hashType">the Algorithm to use to compute the hash</param> /// <returns>a byte[] representation of the hash. If the Stream is a null object /// then null will be returned. If the Stream is empty an empty byte[] {} will be returned.</returns> public static byte[] ComputeHash(this Stream stream, ChecksumAlgorithm hashType = ChecksumAlgorithm.SHA256) { if (stream == null) return null; using (var algorithm = HashAlgorithm.Create(hashType.ToString())) return algorithm.ComputeHash(stream); }
public static bool TryGetChecksumAlgorithm(string checksumName, out ChecksumAlgorithm pdbChecksumAlgorithm, out int checksumSize) { switch (checksumName) { case "SHA1": pdbChecksumAlgorithm = ChecksumAlgorithm.SHA1; checksumSize = 20; return(true); case "SHA256": pdbChecksumAlgorithm = ChecksumAlgorithm.SHA256; checksumSize = 32; return(true); case "SHA384": pdbChecksumAlgorithm = ChecksumAlgorithm.SHA384; checksumSize = 48; return(true); case "SHA512": pdbChecksumAlgorithm = ChecksumAlgorithm.SHA512; checksumSize = 64; return(true); default: pdbChecksumAlgorithm = 0; checksumSize = -1; return(false); } }
public async Task Update(string userId, ArticlePrimaryKey key, UpdateArticleViewModel request) { var article = await _context.Article.FirstOrDefaultAsync(x => x.CreatedYear == key.CreatedYear && x.TitleShrinked == key.TitleShrinked).CAF(); string contentChecksumPreUpdate = ChecksumAlgorithm.ComputeMD5Checksum(article.Content); _mapper.Map(request, article); _articleFactory.SetUpdated(article); await EnsureRequestOfNarrationIfNarratable(article, contentChecksumPreUpdate).CAF(); await using var transaction = await _context.Database.BeginTransactionAsync().CAF(); try { await _context.SaveChangesAsync().CAF(); await transaction.CommitAsync().CAF(); } catch (Exception e) { _logger.LogError(e.Message); throw new FailedOperationException(); } }
/********EXTERNAL OBJECT PUBLIC METHODS - END ********/ private string CalculateCRC(byte[] input, ChecksumAlgorithm checksumAlgorithm) { CRCParameters parms = ChecksumAlgorithmUtils.getParameters(checksumAlgorithm, this.error); if (this.HasError()) { return(""); } long aux = CalculateCRC(input, parms); if (aux == 0 || this.HasError()) { return(""); } switch (parms.Width) { case 8: return(aux.ToString("X2")); case 16: return(aux.ToString("X4")); case 32: return(aux.ToString("X8")); default: return(aux.ToString("X")); } }
/// <summary> /// Computes the hash of a given InputStream. This is a wrapper over the HashAlgorithm crypto functions. /// </summary> /// <param name="stream">the source stream. Use a MemoryStream if uncertain.</param> /// <param name="hashType">the Algorithm to use to compute the hash</param> /// <returns>a byte[] representation of the hash. If the Stream is a null object /// then null will be returned. If the Stream is empty an empty byte[] {} will be returned.</returns> public static byte[] ComputeHash(this Stream stream, ChecksumAlgorithm hashType = ChecksumAlgorithm.SHA256) { if (stream == null) { return(null); } using (var algorithm = HashAlgorithm.Create(hashType.ToString())) return(algorithm.ComputeHash(stream)); }
/// <summary> /// Computes the hash of a given InputStream. This is a wrapper over the HashAlgorithm crypto functions. /// </summary> /// <param name="stream">the source stream. Use a MemoryStream if uncertain.</param> /// <param name="hashType">the Algorithm to use to compute the hash</param> /// <returns>a byte[] representation of the hash. If the Stream is a null object /// then null will be returned. If the Stream is empty an empty byte[] {} will be returned.</returns> public static byte[] ComputeHash(this Stream stream, ChecksumAlgorithm hashType = ChecksumAlgorithm.SHA256, long? maxBodySize = null) { if (stream == null) return null; using (var algorithm = HashAlgorithm.Create(hashType.ToString())) if (maxBodySize != null && maxBodySize > 0) return algorithm.ComputeHash(stream.ReadExactly((long) maxBodySize)); else return algorithm.ComputeHash(stream); }
public static int GetHashStringSize(ChecksumAlgorithm algorithm) { var checksumComputer = GetComputer(algorithm); if (checksumComputer != null) { return(checksumComputer.HashSize / 4); } return(-1); }
public async Task Update(string userId, ArticlePrimaryKey key, UpdateArticleViewModel request) { var article = await(await _client.Article().FindAsync(x => x.Id == key.Id).CAF()) .FirstOrDefaultAsync().CAF(); string contentChecksumPreUpdate = ChecksumAlgorithm.ComputeMD5Checksum(article.Content); _mapper.Map(request, article); _articleFactory.SetUpdated(article); await EnsureRequestOfNarrationIfNarratable(article, contentChecksumPreUpdate).CAF(); await _client.Article().FindOneAndReplaceAsync(x => x.Id == key.Id, article).CAF(); }
public void TestV4SignedHeadersPut(CoreChecksumAlgorithm algorithm) { var putRequest = new PutObjectRequest() { BucketName = _bucketName, Key = $"sigv4-headers-{algorithm}", ContentBody = _testContent, ChecksumAlgorithm = ChecksumAlgorithm.FindValue(algorithm.ToString()), UseChunkEncoding = false }; PutAndGetChecksumTestHelper(algorithm, putRequest); }
public void TestV4aSignedTrailersPut(CoreChecksumAlgorithm algorithm) { var putRequest = new PutObjectRequest() { BucketName = _mrapArn, Key = $"sigv4a-trailers-{algorithm}", ContentBody = _testContent, ChecksumAlgorithm = ChecksumAlgorithm.FindValue(algorithm.ToString()), UseChunkEncoding = true }; PutAndGetChecksumTestHelper(algorithm, putRequest); }
public static bool isHash(ChecksumAlgorithm checksumAlgorithm) { switch (checksumAlgorithm) { case ChecksumAlgorithm.MD5: case ChecksumAlgorithm.SHA1: case ChecksumAlgorithm.SHA256: case ChecksumAlgorithm.SHA512: return(true); default: return(false); } }
private static string GetChecksumName(ChecksumAlgorithm checksumAlgorithm) { switch (checksumAlgorithm) { case ChecksumAlgorithm.Md5: return(Md5AlgName); case ChecksumAlgorithm.Sha1: return(Sha1AlgName); default: throw CodeExceptions.UnexpectedArgumentValue(nameof(checksumAlgorithm), checksumAlgorithm); } }
public void TestV4UnsignedTrailersPut(CoreChecksumAlgorithm algorithm) { var putRequest = new PutObjectRequest() { BucketName = _bucketName, Key = $"sigv4-unsignedcontent-trailers-{algorithm}", ContentBody = _testContent, DisablePayloadSigning = true, ChecksumAlgorithm = ChecksumAlgorithm.FindValue(algorithm.ToString()), UseChunkEncoding = true }; PutAndGetChecksumTestHelper(algorithm, putRequest); }
static HashAlgorithm CreateHasher(ChecksumAlgorithm checksumAlgorithm) { switch (checksumAlgorithm) { case ChecksumAlgorithm.SHA1: return(SHA1.Create()); case ChecksumAlgorithm.SHA256: return(SHA256.Create()); case ChecksumAlgorithm.SHA384: return(SHA384.Create()); case ChecksumAlgorithm.SHA512: return(SHA512.Create()); default: throw new ArgumentOutOfRangeException(nameof(checksumAlgorithm)); } }
/// <summary>Initializes a new instance of the <see cref="SourceAnnotationInfo"/> class.</summary> /// <param name="path">The path to the source file.</param> /// <param name="methodLinesMap">Range that stores start/end lines for each method in the document.</param> /// <param name="sourceLanguage">Language of the source.</param> /// <param name="checksumAlgorithm">The checksum algorithm.</param> /// <param name="checksum">The checksum.</param> public SourceAnnotationInfo( string path, CompositeRange <int, MethodBase> methodLinesMap, SourceLanguage sourceLanguage, ChecksumAlgorithm checksumAlgorithm, byte[] checksum) { Code.NotNullNorEmpty(path, nameof(path)); Code.NotNull(checksum, nameof(checksum)); Path = path; MethodLinesMap = methodLinesMap; SourceLanguage = sourceLanguage; ChecksumAlgorithm = checksumAlgorithm; Checksum = checksum; }
public static string GetChecksumName(ChecksumAlgorithm checksumAlgorithm) { // https://github.com/dotnet/corefx/blob/master/src/System.Reflection.Metadata/specs/PE-COFF.md#pdb-checksum-debug-directory-entry-type-19 switch (checksumAlgorithm) { case ChecksumAlgorithm.SHA1: return("SHA1"); case ChecksumAlgorithm.SHA256: return("SHA256"); case ChecksumAlgorithm.SHA384: return("SHA384"); case ChecksumAlgorithm.SHA512: return("SHA512"); default: throw new ArgumentOutOfRangeException(nameof(checksumAlgorithm)); } }
public static string GetChecksumKey(ChecksumAlgorithm algorithm) { switch (algorithm) { case ChecksumAlgorithm.Sha1: return("Checksum.Sha1"); case ChecksumAlgorithm.Sha256: return("Checksum.Sha256"); case ChecksumAlgorithm.MD5: return("Checksum.MD5"); default: return(null); } }
private string CalculateHash(byte[] input, ChecksumAlgorithm checksumAlgorithm) { HashUtils.HashAlgorithm alg = getHashAlgorithm(checksumAlgorithm); if (this.HasError()) { return(""); } Hashing hash = new Hashing(); byte[] digest = hash.calculateHash(alg, input); if (hash.HasError()) { this.error = hash.GetError(); return(""); } return(toHexaString(digest)); }
/// <summary> /// Computes the hash of a given InputStream. This is a wrapper over the HashAlgorithm crypto functions. /// </summary> /// <param name="stream">the source stream. Use a MemoryStream if uncertain.</param> /// <param name="hashType">the Algorithm to use to compute the hash</param> /// <returns>a byte[] representation of the hash. If the Stream is a null object /// then null will be returned. If the Stream is empty an empty byte[] {} will be returned.</returns> public static byte[] ComputeHash(this Stream stream, ChecksumAlgorithm hashType = ChecksumAlgorithm.SHA256, long?maxBodySize = null) { if (stream == null) { return(null); } using (var algorithm = HashAlgorithm.Create(hashType.ToString())) if (maxBodySize != null && maxBodySize > 0) { return(algorithm.ComputeHash(stream.ReadExactly((long)maxBodySize))); } else { return(algorithm.ComputeHash(stream)); } }
/// <summary> /// Creata a new checksum algorithm from an enum. /// </summary> /// <param name="hashType">The implementation to request.</param> /// <returns>An implementation of the algorithm.</returns> public static HashAlgorithm Create(ChecksumAlgorithm hashType) { switch (hashType) { case ChecksumAlgorithm.SHA256: return(SHA256.Create()); case ChecksumAlgorithm.SHA1: return(SHA1.Create()); case ChecksumAlgorithm.MD5: return(MD5.Create()); default: throw new InvalidOperationException($"Invalid key hash type \"{hashType}\"."); } }
public override unsafe bool GetDebugInfo(ChecksumAlgorithm pdbChecksumAlgorithm, ref uint pdbAge, out Guid guid, out uint stamp, out IMAGE_DEBUG_DIRECTORY pIDD, out byte[] codeViewData) { pIDD = default; codeViewData = null; if (isDeterministic) { ((ISymUnmanagedWriter3)writer).Commit(); var oldPos = pdbStream.Position; pdbStream.Position = 0; var checksumBytes = Hasher.Hash(pdbChecksumAlgorithm, pdbStream, pdbStream.Length); pdbStream.Position = oldPos; if (writer is ISymUnmanagedWriter8 writer8) { RoslynContentIdProvider.GetContentId(checksumBytes, out guid, out stamp); writer8.UpdateSignature(guid, stamp, pdbAge); return(true); } else if (writer is ISymUnmanagedWriter7 writer7) { fixed(byte *p = checksumBytes) writer7.UpdateSignatureByHashingContent(new IntPtr(p), (uint)checksumBytes.Length); } } writer.GetDebugInfo(out pIDD, 0, out uint size, null); codeViewData = new byte[size]; writer.GetDebugInfo(out pIDD, size, out size, codeViewData); if (writer is IPdbWriter comPdbWriter) { var guidBytes = new byte[16]; Array.Copy(codeViewData, 4, guidBytes, 0, 16); guid = new Guid(guidBytes); comPdbWriter.GetSignatureAge(out stamp, out uint age); Debug.Assert(age == pdbAge); pdbAge = age; return(true); } Debug.Fail($"COM PDB writer doesn't impl {nameof(IPdbWriter)}"); guid = default; stamp = 0; return(false); }
/// <summary>Gets checksum for file if it exists.</summary> /// <param name="file">The file.</param> /// <param name="checksumAlgorithm">The checksum algorithm to use.</param> /// <returns>Checksum for file or empty byte array if the file does not exist.</returns> public static byte[] TryGetChecksum( string file, ChecksumAlgorithm checksumAlgorithm) { var algName = GetChecksumName(checksumAlgorithm); if (!File.Exists(file)) { return(Array <byte> .Empty); } using (var f = File.OpenRead(file)) using (var h = HashAlgorithm.Create(algName)) { // ReSharper disable once PossibleNullReferenceException return(h.ComputeHash(f)); } }
public string GenerateChecksum(string input, string inputType, string checksumAlgorithm) { ChecksumInputType chksumInputType = ChecksumInputTypeUtils.getChecksumInputType(inputType, this.error); byte[] inputBytes = ChecksumInputTypeUtils.getBytes(chksumInputType, input, this.error); if (this.HasError()) { return(""); } ChecksumAlgorithm algorithm = ChecksumAlgorithmUtils.getChecksumAlgorithm(checksumAlgorithm, this.error); if (this.HasError()) { return(""); } return((ChecksumAlgorithmUtils.isHash(algorithm)) ? CalculateHash(inputBytes, algorithm) : CalculateCRC(inputBytes, algorithm)); }
private async ValueTask EnsureRequestOfNarrationIfNarratable(Article article, string contentChecksumPreUpdate) { if (!article.Narratable) { return; } string contentChecksumPostUpdate = ChecksumAlgorithm.ComputeMD5Checksum(article.Content); if (contentChecksumPreUpdate == contentChecksumPostUpdate) { return; } var narrationRequest = _articleFactory.CreateNarrationRequest(article, null); using var scope = _serviceScopeFactory.CreateScope(); var channel = scope.ServiceProvider.GetRequiredService <Channel <ArticleNarrationRequest> >(); await channel.Writer.WriteAsync(narrationRequest).ConfigureAwait(false); }
/// <summary>Gets checksum for resource if it exists.</summary> /// <param name="resourceKey">The resource key.</param> /// <param name="checksumAlgorithm">The checksum algorithm to use.</param> /// <returns>Checksum for resource or empty byte array if the resource does not exist.</returns> public static byte[] TryGetChecksum( ResourceKey resourceKey, ChecksumAlgorithm checksumAlgorithm) { var algName = GetChecksumName(checksumAlgorithm); using (var s = resourceKey.TryGetResourceStream()) { if (s == null) { return(Array <byte> .Empty); } using (var h = HashAlgorithm.Create(algName)) { // ReSharper disable once PossibleNullReferenceException return(h.ComputeHash(s)); } } }
public static byte[] Hash(ChecksumAlgorithm checksumAlgorithm, Stream stream, long length) { var buffer = new byte[(int)Math.Min(0x2000, length)]; using (var hasher = CreateHasher(checksumAlgorithm)) { while (length > 0) { int len = (int)Math.Min(length, buffer.Length); int read = stream.Read(buffer, 0, len); if (read == 0) { throw new InvalidOperationException("Couldn't read all bytes"); } hasher.TransformBlock(buffer, 0, read, buffer, 0); length -= read; } hasher.TransformFinalBlock(Array2.Empty <byte>(), 0, 0); return(hasher.Hash); } }
private static Byte[] ComputeOneHundredTwentyEightBitHash(this IEnumerable <Byte> target) => target.Any() ? ChecksumAlgorithm.ComputeHash(target.ToArray()) : EmptyCollectionOneHundredTwentyEightBitHash;
private HashType(ChecksumAlgorithm checksum) { this.Checksum = checksum; }
public static async Task <IReadOnlyDictionary <string, string> > DigestDirectory(string root, ChecksumAlgorithm checksumAlgorithm) { var composeVisitor = new ComposeFileVisitor(); if (Directory.Exists(root)) { var dir = new DirectoryInfo(root); dir.WalkFileTree(composeVisitor); } var dic = new Dictionary <string, string>(); foreach (var fileInfo in composeVisitor.Walked) { await using var stream = fileInfo.OpenRead(); dic.Add(Path.GetRelativePath(root, fileInfo.FullName), checksumAlgorithm switch { ChecksumAlgorithm.SHA256 => Checksum <SHA256CryptoServiceProvider>(stream), ChecksumAlgorithm.MD5 => Checksum <MD5CryptoServiceProvider>(stream), ChecksumAlgorithm.SHA1 => Checksum <SHA1CryptoServiceProvider>(stream), _ => throw new ArgumentException(nameof(checksumAlgorithm)) });
static HashAlgorithm CreateHasher(ChecksumAlgorithm checksumAlgorithm) => checksumAlgorithm switch {
public Checksum(ChecksumAlgorithm algorithm, string checksum) { Algorithm = algorithm; this.checksum = checksum; }
private HashUtils.HashAlgorithm getHashAlgorithm(ChecksumAlgorithm checksumAlgorithm) { return(HashAlgorithmUtils.getHashAlgorithm(ChecksumAlgorithmUtils.valueOf(checksumAlgorithm, this.error), this.error)); }