mirror of
https://github.com/claunia/apprepodbmgr.git
synced 2025-12-16 11:14:40 +00:00
153 lines
5.4 KiB
C#
153 lines
5.4 KiB
C#
//
|
|
// 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
|
|
{
|
|
/// <summary>
|
|
/// Provides a UNIX similar API to .NET SHA256.
|
|
/// </summary>
|
|
public class SHA256Context
|
|
{
|
|
SHA256 _sha256Provider;
|
|
|
|
/// <summary>
|
|
/// Initializes the SHA256 hash provider
|
|
/// </summary>
|
|
public void Init()
|
|
{
|
|
_sha256Provider = SHA256.Create();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the hash with data.
|
|
/// </summary>
|
|
/// <param name="data">Data buffer.</param>
|
|
/// <param name="len">Length of buffer to hash.</param>
|
|
public void Update(byte[] data, uint len)
|
|
{
|
|
_sha256Provider.TransformBlock(data, 0, (int)len, data, 0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the hash with data.
|
|
/// </summary>
|
|
/// <param name="data">Data buffer.</param>
|
|
public void Update(byte[] data)
|
|
{
|
|
Update(data, (uint)data.Length);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a byte array of the hash value.
|
|
/// </summary>
|
|
public byte[] Final()
|
|
{
|
|
_sha256Provider.TransformFinalBlock(new byte[0], 0, 0);
|
|
return _sha256Provider.Hash;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a hexadecimal representation of the hash value.
|
|
/// </summary>
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the hash of a file
|
|
/// </summary>
|
|
/// <param name="filename">File path.</param>
|
|
public byte[] File(string filename)
|
|
{
|
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
|
return _sha256Provider.ComputeHash(fileStream);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the hash of a file in hexadecimal and as a byte array.
|
|
/// </summary>
|
|
/// <param name="filename">File path.</param>
|
|
/// <param name="hash">Byte array of the hash value.</param>
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the hash of the specified data buffer.
|
|
/// </summary>
|
|
/// <param name="data">Data buffer.</param>
|
|
/// <param name="len">Length of the data buffer to hash.</param>
|
|
/// <param name="hash">Byte array of the hash value.</param>
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the hash of the specified data buffer.
|
|
/// </summary>
|
|
/// <param name="data">Data buffer.</param>
|
|
/// <param name="hash">Byte array of the hash value.</param>
|
|
public string Data(byte[] data, out byte[] hash)
|
|
{
|
|
return Data(data, (uint)data.Length, out hash);
|
|
}
|
|
}
|
|
}
|
|
|