// /*************************************************************************** // Aaru Data Preservation Suite // ---------------------------------------------------------------------------- // // Filename : FletcherContext.cs // Author(s) : Natalia Portillo // // 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 . // // ---------------------------------------------------------------------------- // Copyright © 2011-2020 Natalia Portillo // ****************************************************************************/ // Disabled because the speed is abnormally slow using System.IO; using System.Text; using Aaru.CommonTypes.Interfaces; namespace Aaru.Checksums { /// Implements the Fletcher-32 algorithm public class Fletcher32Context : IChecksum { const ushort FLETCHER_MODULE = 0xFFFF; ushort sum1, sum2; /// Initializes the Fletcher-32 sums public Fletcher32Context() { sum1 = 0xFFFF; sum2 = 0xFFFF; } /// Updates the hash with data. /// Data buffer. /// Length of buffer to hash. public void Update(byte[] data, uint len) { for(int i = 0; i < len; i++) { sum1 = (ushort)((sum1 + data[i]) % FLETCHER_MODULE); sum2 = (ushort)((sum2 + sum1) % FLETCHER_MODULE); } } /// 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() { uint finalSum = (uint)((sum2 << 16) | sum1); return BigEndianBitConverter.GetBytes(finalSum); } /// Returns a hexadecimal representation of the hash value. public string End() { uint finalSum = (uint)((sum2 << 16) | sum1); var fletcherOutput = new StringBuilder(); for(int i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++) fletcherOutput.Append(BigEndianBitConverter.GetBytes(finalSum)[i].ToString("x2")); return fletcherOutput.ToString(); } /// Gets the hash of a file /// File path. public static byte[] File(string filename) { File(filename, out byte[] hash); return hash; } /// Gets the hash of a file in hexadecimal and as a byte array. /// File path. /// Byte array of the hash value. public static string File(string filename, out byte[] hash) { var fileStream = new FileStream(filename, FileMode.Open); ushort localSum1 = 0xFFFF; ushort localSum2 = 0xFFFF; for(int i = 0; i < fileStream.Length; i++) { localSum1 = (ushort)((localSum1 + fileStream.ReadByte()) % FLETCHER_MODULE); localSum2 = (ushort)((localSum2 + localSum1) % FLETCHER_MODULE); } uint finalSum = (uint)((localSum2 << 16) | localSum1); hash = BigEndianBitConverter.GetBytes(finalSum); var fletcherOutput = new StringBuilder(); foreach(byte h in hash) fletcherOutput.Append(h.ToString("x2")); fileStream.Close(); return fletcherOutput.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 static string Data(byte[] data, uint len, out byte[] hash) { ushort localSum1 = 0xFFFF; ushort localSum2 = 0xFFFF; for(int i = 0; i < len; i++) { localSum1 = (ushort)((localSum1 + data[i]) % FLETCHER_MODULE); localSum2 = (ushort)((localSum2 + localSum1) % FLETCHER_MODULE); } uint finalSum = (uint)((localSum2 << 16) | localSum1); hash = BigEndianBitConverter.GetBytes(finalSum); var adlerOutput = new StringBuilder(); foreach(byte h in hash) adlerOutput.Append(h.ToString("x2")); return adlerOutput.ToString(); } /// Gets the hash of the specified data buffer. /// Data buffer. /// Byte array of the hash value. public static string Data(byte[] data, out byte[] hash) => Data(data, (uint)data.Length, out hash); } /// Implements the Fletcher-16 algorithm public class Fletcher16Context : IChecksum { const byte FLETCHER_MODULE = 0xFF; byte sum1, sum2; /// Initializes the Fletcher-16 sums public Fletcher16Context() { sum1 = 0xFF; sum2 = 0xFF; } /// Updates the hash with data. /// Data buffer. /// Length of buffer to hash. public void Update(byte[] data, uint len) { for(int i = 0; i < len; i++) { sum1 = (byte)((sum1 + data[i]) % FLETCHER_MODULE); sum2 = (byte)((sum2 + sum1) % FLETCHER_MODULE); } } /// 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() { ushort finalSum = (ushort)((sum2 << 8) | sum1); return BigEndianBitConverter.GetBytes(finalSum); } /// Returns a hexadecimal representation of the hash value. public string End() { ushort finalSum = (ushort)((sum2 << 8) | sum1); var fletcherOutput = new StringBuilder(); for(int i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++) fletcherOutput.Append(BigEndianBitConverter.GetBytes(finalSum)[i].ToString("x2")); return fletcherOutput.ToString(); } /// Gets the hash of a file /// File path. public static byte[] File(string filename) { File(filename, out byte[] hash); return hash; } /// Gets the hash of a file in hexadecimal and as a byte array. /// File path. /// Byte array of the hash value. public static string File(string filename, out byte[] hash) { var fileStream = new FileStream(filename, FileMode.Open); byte localSum1 = 0xFF; byte localSum2 = 0xFF; for(int i = 0; i < fileStream.Length; i++) { localSum1 = (byte)((localSum1 + fileStream.ReadByte()) % FLETCHER_MODULE); localSum2 = (byte)((localSum2 + localSum1) % FLETCHER_MODULE); } ushort finalSum = (ushort)((localSum2 << 8) | localSum1); hash = BigEndianBitConverter.GetBytes(finalSum); var fletcherOutput = new StringBuilder(); foreach(byte h in hash) fletcherOutput.Append(h.ToString("x2")); fileStream.Close(); return fletcherOutput.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 static string Data(byte[] data, uint len, out byte[] hash) { byte localSum1 = 0xFF; byte localSum2 = 0xFF; for(int i = 0; i < len; i++) { localSum1 = (byte)((localSum1 + data[i]) % FLETCHER_MODULE); localSum2 = (byte)((localSum2 + localSum1) % FLETCHER_MODULE); } ushort finalSum = (ushort)((localSum2 << 8) | localSum1); hash = BigEndianBitConverter.GetBytes(finalSum); var adlerOutput = new StringBuilder(); foreach(byte h in hash) adlerOutput.Append(h.ToString("x2")); return adlerOutput.ToString(); } /// Gets the hash of the specified data buffer. /// Data buffer. /// Byte array of the hash value. public static string Data(byte[] data, out byte[] hash) => Data(data, (uint)data.Length, out hash); } }