|
/*
* ---------------------------------------------------------
* SHA256Wrappers.cs
*
* by Alphons van der Heijden
* ---------------------------------------------------------
* Comments:
* Some simple wrappers so it works
* as in System.Security.Cryptography
* the Axantum.PasswordSafe likes it this way
* especialy in PasswordSafeReader.cs
* ---------------------------------------------------------
*
*/
using System;
namespace Helpers.PasswordSafe
{
/// <summary>
/// Wrapper to make it behave the same way as Axantum.PasswordSafe likes
/// </summary>
public class SHA256 : IDisposable
{
/// <summary>
/// helper function not to rely on System.Text.ASCIIEncoder
/// </summary>
public static byte[] ASCIIEncoder(string s)
{
byte[] ascii = new byte[s.Length];
for (int i = 0; i < s.Length; i++)
{
ascii[i] = (byte)s[i];
}
return ascii;
}
public SHA256()
{
Clear();
}
public static SHA256 Create()
{
return new SHA256();
}
public byte[] ComputeHash(byte[] input)
{
return SHA256Class.MessageSHA256(input);
}
void System.IDisposable.Dispose()
{
}
private byte[] m_StartBlock;
private byte[] m_Hash;
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
m_StartBlock = new byte[inputCount];
Array.Copy(inputBuffer, inputOffset, m_StartBlock, 0, inputCount);
//byte[] data = new byte[m_StartBlock.Length + inputCount];
//Array.Copy(m_StartBlock, 0, data, 0, m_StartBlock.Length);
//Array.Copy(inputBuffer, inputOffset, data, m_StartBlock.Length, inputCount);
//m_StartBlock = SHA256Class.MessageSHA256(data);
return inputCount;
}
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
{
byte[] data = new byte[m_StartBlock.Length + inputCount];
Array.Copy(m_StartBlock, 0, data, 0, m_StartBlock.Length);
Array.Copy(inputBuffer, inputOffset, data, m_StartBlock.Length, inputCount);
data = SHA256Class.MessageSHA256(data);
m_Hash = new byte[32];
Array.Copy(data, 0, m_Hash, 0, 32);
return inputBuffer;
}
public byte[] Hash
{
get
{
return m_Hash;
}
}
public void Clear()
{
m_StartBlock = new byte[0];
m_Hash = new byte[0];
}
}
public class SHA256Managed : SHA256
{
}
/// <summary>
/// More or less a dummy class,
/// The TransformBlock only does init the startblock
/// it can not be used as it should be
/// therefore the compare in ReadHmac()
/// in PasswordSafeReader.cs fails!!
/// </summary>
public class HMACSHA256 : SHA256
{
public HMACSHA256(byte[] hmacKeyBytes)
{
this.TransformBlock(hmacKeyBytes, 0, hmacKeyBytes.Length, null, 0);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I'm Alphons van der Heijden, living in Lelystad, Netherlands, Europa, Earth. And currently I'm retiring from hard working ( ;- ), owning my own company. Because I'm full of energy, and a little to young to relax ...., I don't sit down, but create and recreate software solutions, that I like. Reinventing the wheel is my second nature. My interest is in the area of Internet technologies, .NET etc. I was there in 1992 when Mosaic came out, and from that point, my life changed dramatically, and so did the world, in fact. (Y)
I've been working with all aspects of software development since 1979 - from compiler construction to management. Currently I'm an independent consultant mostly specializing in computer security. Please see my homepage for contact details.
I speak C like a native, and have a pretty good grasp of C++. The most recent five years C# has been the main development language. Traditionally Unix has been the dominating environment, but currently the scales have tipped over to Windows, due to market demands but I'm equally at home developing in both environments.
When I'm not coding I'm usually sitting on one of my 4 bikes, indoors or outdoors, on the road or in the woods.
I am currently employed as a C# / MS SQL developer for a Medical Software company working on a large enterprise system.
I have used Delphi and MSM MUMPS in previous roles and love both languages.
I'm an avid Windows Mobile fan and moderator on http://xda-developers.com. I code in C#, and C++ for WM as well as cooking custom ROMs and learning to disasm the inners of my device.
I have some intensive experience with C#, ASP.NET and Delphi. Also I like to write API's, which I realized when I made a SmartCard library.