// // Author: // Natalia Portillo claunia@claunia.com // // Copyright (c) 2017, © Claunia.com // // All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in // the documentation and/or other materials provided with the distribution. // * Neither the name of the [ORGANIZATION] nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // using System.IO; using System.Security.Cryptography; using System.Text; namespace osrepodbmgr { /// /// Provides a UNIX similar API to .NET SHA256. /// public class SHA256Context { SHA256 _sha256Provider; /// /// Initializes the SHA256 hash provider /// public void Init() { _sha256Provider = SHA256.Create(); } /// /// Updates the hash with data. /// /// Data buffer. /// Length of buffer to hash. public void Update(byte[] data, uint len) { _sha256Provider.TransformBlock(data, 0, (int)len, data, 0); } /// /// Updates the hash with data. /// /// Data buffer. public void Update(byte[] data) { Update(data, (uint)data.Length); } /// /// Returns a byte array of the hash value. /// public byte[] Final() { _sha256Provider.TransformFinalBlock(new byte[0], 0, 0); return _sha256Provider.Hash; } /// /// Returns a hexadecimal representation of the hash value. /// public string End() { _sha256Provider.TransformFinalBlock(new byte[0], 0, 0); StringBuilder sha256Output = new StringBuilder(); for(int i = 0; i < _sha256Provider.Hash.Length; i++) { sha256Output.Append(_sha256Provider.Hash[i].ToString("x2")); } return sha256Output.ToString(); } /// /// Gets the hash of a file /// /// File path. public byte[] File(string filename) { FileStream fileStream = new FileStream(filename, FileMode.Open); return _sha256Provider.ComputeHash(fileStream); } /// /// Gets the hash of a file in hexadecimal and as a byte array. /// /// File path. /// Byte array of the hash value. public string File(string filename, out byte[] hash) { FileStream fileStream = new FileStream(filename, FileMode.Open); hash = _sha256Provider.ComputeHash(fileStream); StringBuilder sha256Output = new StringBuilder(); for(int i = 0; i < hash.Length; i++) { sha256Output.Append(hash[i].ToString("x2")); } return sha256Output.ToString(); } /// /// Gets the hash of the specified data buffer. /// /// Data buffer. /// Length of the data buffer to hash. /// Byte array of the hash value. public string Data(byte[] data, uint len, out byte[] hash) { hash = _sha256Provider.ComputeHash(data, 0, (int)len); StringBuilder sha256Output = new StringBuilder(); for(int i = 0; i < hash.Length; i++) { sha256Output.Append(hash[i].ToString("x2")); } return sha256Output.ToString(); } /// /// Gets the hash of the specified data buffer. /// /// Data buffer. /// Byte array of the hash value. public string Data(byte[] data, out byte[] hash) { return Data(data, (uint)data.Length, out hash); } } }