2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2016-07-28 18:13:49 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : FletcherContext.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : Checksums.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Implements Fletcher-32 and Fletcher-16 algorithms.
|
|
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// This library is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
// published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This library is distributed in the hope that it will be useful, but
|
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2016-07-28 18:13:49 +01:00
|
|
|
// ****************************************************************************/
|
2015-11-30 23:00:47 +00:00
|
|
|
|
|
|
|
|
// Disabled because the speed is abnormally slow
|
2018-02-03 17:01:17 +00:00
|
|
|
|
2015-04-19 01:18:36 +01:00
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2017-12-19 19:33:46 +00:00
|
|
|
using System.Text;
|
2015-04-19 01:18:36 +01:00
|
|
|
|
|
|
|
|
namespace DiscImageChef.Checksums
|
|
|
|
|
{
|
2018-02-03 17:01:17 +00:00
|
|
|
public class Fletcher32Context : IChecksum
|
2015-04-19 01:18:36 +01:00
|
|
|
{
|
2018-02-03 17:39:49 +00:00
|
|
|
bool inodd;
|
|
|
|
|
byte oddValue;
|
2017-12-23 01:46:08 +00:00
|
|
|
ushort sum1, sum2;
|
2015-04-19 01:18:36 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 01:46:08 +00:00
|
|
|
/// Initializes the Fletcher32 sums
|
2015-04-19 01:18:36 +01:00
|
|
|
/// </summary>
|
2018-02-03 17:39:49 +00:00
|
|
|
public Fletcher32Context()
|
2015-04-19 01:18:36 +01:00
|
|
|
{
|
2018-02-03 17:39:49 +00:00
|
|
|
sum1 = 0xFFFF;
|
|
|
|
|
sum2 = 0xFFFF;
|
2015-04-19 01:18:36 +01:00
|
|
|
oddValue = 0;
|
2018-02-03 17:39:49 +00:00
|
|
|
inodd = false;
|
2015-04-19 01:18:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 01:46:08 +00:00
|
|
|
/// Updates the hash with data.
|
2015-04-19 01:18:36 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">Data buffer.</param>
|
|
|
|
|
/// <param name="len">Length of buffer to hash.</param>
|
|
|
|
|
public void Update(byte[] data, uint len)
|
|
|
|
|
{
|
2017-12-23 01:46:08 +00:00
|
|
|
ushort block;
|
2016-04-19 02:11:47 +01:00
|
|
|
if(!inodd)
|
|
|
|
|
if(len % 2 != 0)
|
2015-04-19 01:18:36 +01:00
|
|
|
{
|
|
|
|
|
oddValue = data[len - 1];
|
2018-02-03 17:39:49 +00:00
|
|
|
inodd = true;
|
2015-04-19 01:18:36 +01:00
|
|
|
|
2016-04-19 02:11:47 +01:00
|
|
|
for(int i = 0; i < len - 1; i += 2)
|
2015-04-19 01:18:36 +01:00
|
|
|
{
|
|
|
|
|
block = BigEndianBitConverter.ToUInt16(data, i);
|
2018-02-03 17:39:49 +00:00
|
|
|
sum1 = (ushort)((sum1 + block) % 0xFFFF);
|
|
|
|
|
sum2 = (ushort)((sum2 + sum1) % 0xFFFF);
|
2015-04-19 01:18:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
inodd = false;
|
2016-04-19 02:11:47 +01:00
|
|
|
for(int i = 0; i < len; i += 2)
|
2015-04-19 01:18:36 +01:00
|
|
|
{
|
|
|
|
|
block = BigEndianBitConverter.ToUInt16(data, i);
|
2018-02-03 17:39:49 +00:00
|
|
|
sum1 = (ushort)((sum1 + block) % 0xFFFF);
|
|
|
|
|
sum2 = (ushort)((sum2 + sum1) % 0xFFFF);
|
2015-04-19 01:18:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Carrying odd
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
byte[] oddData = new byte[2];
|
2018-02-03 17:39:49 +00:00
|
|
|
oddData[0] = oddValue;
|
|
|
|
|
oddData[1] = data[0];
|
2015-04-19 01:18:36 +01:00
|
|
|
|
|
|
|
|
block = BigEndianBitConverter.ToUInt16(oddData, 0);
|
2018-02-03 17:39:49 +00:00
|
|
|
sum1 = (ushort)((sum1 + block) % 0xFFFF);
|
|
|
|
|
sum2 = (ushort)((sum2 + sum1) % 0xFFFF);
|
2015-04-19 01:18:36 +01:00
|
|
|
|
|
|
|
|
// Even size, carrying odd
|
2016-04-19 02:11:47 +01:00
|
|
|
if(len % 2 == 0)
|
2015-04-19 01:18:36 +01:00
|
|
|
{
|
|
|
|
|
oddValue = data[len - 1];
|
2018-02-03 17:39:49 +00:00
|
|
|
inodd = true;
|
2015-04-19 01:18:36 +01:00
|
|
|
|
2016-04-19 02:11:47 +01:00
|
|
|
for(int i = 1; i < len - 1; i += 2)
|
2015-04-19 01:18:36 +01:00
|
|
|
{
|
|
|
|
|
block = BigEndianBitConverter.ToUInt16(data, i);
|
2018-02-03 17:39:49 +00:00
|
|
|
sum1 = (ushort)((sum1 + block) % 0xFFFF);
|
|
|
|
|
sum2 = (ushort)((sum2 + sum1) % 0xFFFF);
|
2015-04-19 01:18:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
inodd = false;
|
2016-04-19 02:11:47 +01:00
|
|
|
for(int i = 1; i < len; i += 2)
|
2015-04-19 01:18:36 +01:00
|
|
|
{
|
|
|
|
|
block = BigEndianBitConverter.ToUInt16(data, i);
|
2018-02-03 17:39:49 +00:00
|
|
|
sum1 = (ushort)((sum1 + block) % 0xFFFF);
|
|
|
|
|
sum2 = (ushort)((sum2 + sum1) % 0xFFFF);
|
2015-04-19 01:18:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 01:46:08 +00:00
|
|
|
/// Updates the hash with data.
|
2015-04-19 01:18:36 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">Data buffer.</param>
|
|
|
|
|
public void Update(byte[] data)
|
|
|
|
|
{
|
|
|
|
|
Update(data, (uint)data.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 01:46:08 +00:00
|
|
|
/// Returns a byte array of the hash value.
|
2015-04-19 01:18:36 +01:00
|
|
|
/// </summary>
|
|
|
|
|
public byte[] Final()
|
|
|
|
|
{
|
2017-12-23 01:46:08 +00:00
|
|
|
uint finalSum = (uint)(sum1 + (sum2 << 16));
|
2015-04-19 01:18:36 +01:00
|
|
|
return BigEndianBitConverter.GetBytes(finalSum);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 01:46:08 +00:00
|
|
|
/// Returns a hexadecimal representation of the hash value.
|
2015-04-19 01:18:36 +01:00
|
|
|
/// </summary>
|
|
|
|
|
public string End()
|
|
|
|
|
{
|
2018-02-03 17:39:49 +00:00
|
|
|
uint finalSum = (uint)(sum1 + (sum2 << 16));
|
2015-04-19 01:18:36 +01:00
|
|
|
StringBuilder fletcherOutput = new StringBuilder();
|
|
|
|
|
|
2016-04-19 02:11:47 +01:00
|
|
|
for(int i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++)
|
2015-04-19 01:18:36 +01:00
|
|
|
fletcherOutput.Append(BigEndianBitConverter.GetBytes(finalSum)[i].ToString("x2"));
|
|
|
|
|
|
|
|
|
|
return fletcherOutput.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 01:46:08 +00:00
|
|
|
/// Gets the hash of a file
|
2015-04-19 01:18:36 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">File path.</param>
|
|
|
|
|
public static byte[] File(string filename)
|
|
|
|
|
{
|
2017-12-23 01:46:08 +00:00
|
|
|
File(filename, out byte[] hash);
|
2015-04-19 01:18:36 +01:00
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 01:46:08 +00:00
|
|
|
/// Gets the hash of a file in hexadecimal and as a byte array.
|
2015-04-19 01:18:36 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">File path.</param>
|
|
|
|
|
/// <param name="hash">Byte array of the hash value.</param>
|
|
|
|
|
public static string File(string filename, out byte[] hash)
|
|
|
|
|
{
|
|
|
|
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
2018-02-03 17:39:49 +00:00
|
|
|
ushort localSum1, localSum2, block;
|
|
|
|
|
uint finalSum;
|
|
|
|
|
byte[] blockBytes;
|
2015-04-19 01:18:36 +01:00
|
|
|
|
|
|
|
|
localSum1 = 0xFFFF;
|
|
|
|
|
localSum2 = 0xFFFF;
|
|
|
|
|
|
2016-04-19 02:11:47 +01:00
|
|
|
if(fileStream.Length % 2 == 0)
|
2018-02-03 17:39:49 +00:00
|
|
|
for(int i = 0; i < fileStream.Length; i += 2)
|
2015-04-19 01:18:36 +01:00
|
|
|
{
|
|
|
|
|
blockBytes = new byte[2];
|
|
|
|
|
fileStream.Read(blockBytes, 0, 2);
|
2018-02-03 17:39:49 +00:00
|
|
|
block = BigEndianBitConverter.ToUInt16(blockBytes, 0);
|
|
|
|
|
localSum1 = (ushort)((localSum1 + block) % 0xFFFF);
|
2017-12-23 01:46:08 +00:00
|
|
|
localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF);
|
2015-04-19 01:18:36 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-04-19 02:11:47 +01:00
|
|
|
for(int i = 0; i < fileStream.Length - 1; i += 2)
|
2015-04-19 01:18:36 +01:00
|
|
|
{
|
|
|
|
|
blockBytes = new byte[2];
|
|
|
|
|
fileStream.Read(blockBytes, 0, 2);
|
2018-02-03 17:39:49 +00:00
|
|
|
block = BigEndianBitConverter.ToUInt16(blockBytes, 0);
|
|
|
|
|
localSum1 = (ushort)((localSum1 + block) % 0xFFFF);
|
2017-12-23 01:46:08 +00:00
|
|
|
localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF);
|
2015-04-19 01:18:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] oddData = new byte[2];
|
2018-02-03 17:39:49 +00:00
|
|
|
oddData[0] = (byte)fileStream.ReadByte();
|
|
|
|
|
oddData[1] = 0;
|
2015-04-19 01:18:36 +01:00
|
|
|
|
2018-02-03 17:39:49 +00:00
|
|
|
block = BigEndianBitConverter.ToUInt16(oddData, 0);
|
|
|
|
|
localSum1 = (ushort)((localSum1 + block) % 0xFFFF);
|
2017-12-23 01:46:08 +00:00
|
|
|
localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF);
|
2015-04-19 01:18:36 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-23 01:46:08 +00:00
|
|
|
finalSum = (uint)(localSum1 + (localSum2 << 16));
|
2015-04-19 01:18:36 +01:00
|
|
|
|
|
|
|
|
hash = BitConverter.GetBytes(finalSum);
|
|
|
|
|
|
|
|
|
|
StringBuilder fletcherOutput = new StringBuilder();
|
|
|
|
|
|
2017-12-23 01:46:08 +00:00
|
|
|
foreach(byte h in hash) fletcherOutput.Append(h.ToString("x2"));
|
2015-04-19 01:18:36 +01:00
|
|
|
|
|
|
|
|
return fletcherOutput.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 01:46:08 +00:00
|
|
|
/// Gets the hash of the specified data buffer.
|
2015-04-19 01:18:36 +01:00
|
|
|
/// </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 static string Data(byte[] data, uint len, out byte[] hash)
|
|
|
|
|
{
|
2017-12-23 01:46:08 +00:00
|
|
|
ushort localSum1, localSum2, block;
|
2018-02-03 17:39:49 +00:00
|
|
|
uint finalSum;
|
2015-04-19 01:18:36 +01:00
|
|
|
|
|
|
|
|
localSum1 = 0xFFFF;
|
|
|
|
|
localSum2 = 0xFFFF;
|
|
|
|
|
|
2018-02-03 17:39:49 +00:00
|
|
|
if(len % 2 == 0)
|
2016-04-19 02:11:47 +01:00
|
|
|
for(int i = 0; i < len; i += 2)
|
2015-04-19 01:18:36 +01:00
|
|
|
{
|
2018-02-03 17:39:49 +00:00
|
|
|
block = BigEndianBitConverter.ToUInt16(data, i);
|
|
|
|
|
localSum1 = (ushort)((localSum1 + block) % 0xFFFF);
|
2017-12-23 01:46:08 +00:00
|
|
|
localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF);
|
2015-04-19 01:18:36 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-04-19 02:11:47 +01:00
|
|
|
for(int i = 0; i < len - 1; i += 2)
|
2015-04-19 01:18:36 +01:00
|
|
|
{
|
2018-02-03 17:39:49 +00:00
|
|
|
block = BigEndianBitConverter.ToUInt16(data, i);
|
|
|
|
|
localSum1 = (ushort)((localSum1 + block) % 0xFFFF);
|
2017-12-23 01:46:08 +00:00
|
|
|
localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF);
|
2015-04-19 01:18:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] oddData = new byte[2];
|
2018-02-03 17:39:49 +00:00
|
|
|
oddData[0] = data[len - 1];
|
|
|
|
|
oddData[1] = 0;
|
2015-04-19 01:18:36 +01:00
|
|
|
|
2018-02-03 17:39:49 +00:00
|
|
|
block = BigEndianBitConverter.ToUInt16(oddData, 0);
|
|
|
|
|
localSum1 = (ushort)((localSum1 + block) % 0xFFFF);
|
2017-12-23 01:46:08 +00:00
|
|
|
localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF);
|
2015-04-19 01:18:36 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-23 01:46:08 +00:00
|
|
|
finalSum = (uint)(localSum1 + (localSum2 << 16));
|
2015-04-19 01:18:36 +01:00
|
|
|
|
|
|
|
|
hash = BitConverter.GetBytes(finalSum);
|
|
|
|
|
|
|
|
|
|
StringBuilder fletcherOutput = new StringBuilder();
|
|
|
|
|
|
2017-12-23 01:46:08 +00:00
|
|
|
foreach(byte h in hash) fletcherOutput.Append(h.ToString("x2"));
|
2015-04-19 01:18:36 +01:00
|
|
|
|
|
|
|
|
return fletcherOutput.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 01:46:08 +00:00
|
|
|
/// Gets the hash of the specified data buffer.
|
2015-04-19 01:18:36 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">Data buffer.</param>
|
|
|
|
|
/// <param name="hash">Byte array of the hash value.</param>
|
|
|
|
|
public static string Data(byte[] data, out byte[] hash)
|
|
|
|
|
{
|
|
|
|
|
return Data(data, (uint)data.Length, out hash);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Fletcher16Context
|
|
|
|
|
{
|
|
|
|
|
byte sum1, sum2;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 01:46:08 +00:00
|
|
|
/// Initializes the Fletcher16 sums
|
2015-04-19 01:18:36 +01:00
|
|
|
/// </summary>
|
|
|
|
|
public void Init()
|
|
|
|
|
{
|
|
|
|
|
sum1 = 0xFF;
|
|
|
|
|
sum2 = 0xFF;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 01:46:08 +00:00
|
|
|
/// Updates the hash with data.
|
2015-04-19 01:18:36 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">Data buffer.</param>
|
|
|
|
|
/// <param name="len">Length of buffer to hash.</param>
|
|
|
|
|
public void Update(byte[] data, uint len)
|
|
|
|
|
{
|
2016-04-19 02:11:47 +01:00
|
|
|
for(int i = 0; i < len; i++)
|
2015-04-19 01:18:36 +01:00
|
|
|
{
|
|
|
|
|
sum1 = (byte)((sum1 + data[i]) % 0xFF);
|
2018-02-03 17:39:49 +00:00
|
|
|
sum2 = (byte)((sum2 + sum1) % 0xFF);
|
2015-04-19 01:18:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 01:46:08 +00:00
|
|
|
/// Updates the hash with data.
|
2015-04-19 01:18:36 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">Data buffer.</param>
|
|
|
|
|
public void Update(byte[] data)
|
|
|
|
|
{
|
|
|
|
|
Update(data, (uint)data.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 01:46:08 +00:00
|
|
|
/// Returns a byte array of the hash value.
|
2015-04-19 01:18:36 +01:00
|
|
|
/// </summary>
|
|
|
|
|
public byte[] Final()
|
|
|
|
|
{
|
2017-12-23 01:46:08 +00:00
|
|
|
ushort finalSum = (ushort)(sum1 + (sum2 << 8));
|
2015-04-19 01:18:36 +01:00
|
|
|
return BigEndianBitConverter.GetBytes(finalSum);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 01:46:08 +00:00
|
|
|
/// Returns a hexadecimal representation of the hash value.
|
2015-04-19 01:18:36 +01:00
|
|
|
/// </summary>
|
|
|
|
|
public string End()
|
|
|
|
|
{
|
2018-02-03 17:39:49 +00:00
|
|
|
ushort finalSum = (ushort)(sum1 + (sum2 << 8));
|
2015-04-19 01:18:36 +01:00
|
|
|
StringBuilder fletcherOutput = new StringBuilder();
|
|
|
|
|
|
2016-04-19 02:11:47 +01:00
|
|
|
for(int i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++)
|
2015-04-19 01:18:36 +01:00
|
|
|
fletcherOutput.Append(BigEndianBitConverter.GetBytes(finalSum)[i].ToString("x2"));
|
|
|
|
|
|
|
|
|
|
return fletcherOutput.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 01:46:08 +00:00
|
|
|
/// Gets the hash of a file
|
2015-04-19 01:18:36 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">File path.</param>
|
|
|
|
|
public static byte[] File(string filename)
|
|
|
|
|
{
|
2017-12-23 01:46:08 +00:00
|
|
|
File(filename, out byte[] hash);
|
2015-04-19 01:18:36 +01:00
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 01:46:08 +00:00
|
|
|
/// Gets the hash of a file in hexadecimal and as a byte array.
|
2015-04-19 01:18:36 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">File path.</param>
|
|
|
|
|
/// <param name="hash">Byte array of the hash value.</param>
|
|
|
|
|
public static string File(string filename, out byte[] hash)
|
|
|
|
|
{
|
|
|
|
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
2018-02-03 17:39:49 +00:00
|
|
|
byte localSum1, localSum2, block;
|
|
|
|
|
ushort finalSum;
|
2015-04-19 01:18:36 +01:00
|
|
|
|
|
|
|
|
localSum1 = 0xFF;
|
|
|
|
|
localSum2 = 0xFF;
|
|
|
|
|
|
2016-04-19 02:11:47 +01:00
|
|
|
for(int i = 0; i < fileStream.Length; i += 2)
|
2015-04-19 01:18:36 +01:00
|
|
|
{
|
2018-02-03 17:39:49 +00:00
|
|
|
block = (byte)fileStream.ReadByte();
|
|
|
|
|
localSum1 = (byte)((localSum1 + block) % 0xFF);
|
2015-04-19 01:18:36 +01:00
|
|
|
localSum2 = (byte)((localSum2 + localSum1) % 0xFF);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-23 01:46:08 +00:00
|
|
|
finalSum = (ushort)(localSum1 + (localSum2 << 8));
|
2015-04-19 01:18:36 +01:00
|
|
|
|
|
|
|
|
hash = BitConverter.GetBytes(finalSum);
|
|
|
|
|
|
|
|
|
|
StringBuilder fletcherOutput = new StringBuilder();
|
|
|
|
|
|
2017-12-23 01:46:08 +00:00
|
|
|
foreach(byte h in hash) fletcherOutput.Append(h.ToString("x2"));
|
2015-04-19 01:18:36 +01:00
|
|
|
|
|
|
|
|
return fletcherOutput.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 01:46:08 +00:00
|
|
|
/// Gets the hash of the specified data buffer.
|
2015-04-19 01:18:36 +01:00
|
|
|
/// </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 static string Data(byte[] data, uint len, out byte[] hash)
|
|
|
|
|
{
|
2018-02-03 17:39:49 +00:00
|
|
|
byte localSum1, localSum2;
|
2017-12-23 01:46:08 +00:00
|
|
|
ushort finalSum;
|
2015-04-19 01:18:36 +01:00
|
|
|
|
|
|
|
|
localSum1 = 0xFF;
|
|
|
|
|
localSum2 = 0xFF;
|
|
|
|
|
|
2016-04-19 02:11:47 +01:00
|
|
|
for(int i = 0; i < len; i++)
|
2015-04-19 01:18:36 +01:00
|
|
|
{
|
2018-02-03 17:39:49 +00:00
|
|
|
localSum1 = (byte)((localSum1 + data[i]) % 0xFF);
|
2015-04-19 01:18:36 +01:00
|
|
|
localSum2 = (byte)((localSum2 + localSum1) % 0xFF);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-23 01:46:08 +00:00
|
|
|
finalSum = (ushort)(localSum1 + (localSum2 << 8));
|
2015-04-19 01:18:36 +01:00
|
|
|
|
|
|
|
|
hash = BitConverter.GetBytes(finalSum);
|
|
|
|
|
|
|
|
|
|
StringBuilder fletcherOutput = new StringBuilder();
|
|
|
|
|
|
2017-12-23 01:46:08 +00:00
|
|
|
foreach(byte h in hash) fletcherOutput.Append(h.ToString("x2"));
|
2015-04-19 01:18:36 +01:00
|
|
|
|
|
|
|
|
return fletcherOutput.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 01:46:08 +00:00
|
|
|
/// Gets the hash of the specified data buffer.
|
2015-04-19 01:18:36 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">Data buffer.</param>
|
|
|
|
|
/// <param name="hash">Byte array of the hash value.</param>
|
|
|
|
|
public static string Data(byte[] data, out byte[] hash)
|
|
|
|
|
{
|
|
|
|
|
return Data(data, (uint)data.Length, out hash);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-03 17:01:17 +00:00
|
|
|
}
|