{***************************************************************************** The DEC team (see file NOTICE.txt) licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. A copy of this licence is found in the root directory of this project in the file LICENCE.txt or alternatively at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *****************************************************************************} unit DECHashInterface; interface uses {$IFDEF FPC} SysUtils, Classes, {$ELSE} System.SysUtils, System.Classes, {$ENDIF} DECFormat, DECUtil; type /// /// Generic interface for all hash classes. /// Unfortunately without all the class methods, as they are not accepted /// in interface declarations /// IDECHash = Interface /// /// Generic initialization of internal data structures. Additionally the /// internal algorithm specific (because of being overridden by each /// hash algorithm) DoInit method. Needs to be called before each hash /// calculation. /// procedure Init; /// /// Calculates one chunk of data to be hashed. /// /// /// Data on which the hash value shall be calculated on /// /// /// Size of the data in bytes /// procedure Calc(const Data; DataSize: Integer); /// /// Frees dynamically allocated buffers in a way which safeguards agains /// data stealing by other methods which afterwards might allocate this memory. /// Additionaly calls the algorithm spercific DoDone method. /// procedure Done; /// /// Returns the calculated hash value as byte array /// function DigestAsBytes: TBytes; /// /// Returns the calculated hash value as formatted Unicode string /// /// /// Optional parameter. If a formatting class is being passed the formatting /// will be applied to the returned string. Otherwise no formatting is /// being used. /// /// /// Hash value of the last performed hash calculation /// /// /// We recommend to use a formatting which results in 7 bit ASCII chars /// being returned, otherwise the conversion into the Unicode string might /// result in strange characters in the returned result. /// function DigestAsString(Format: TDECFormatClass = nil): string; /// /// Returns the calculated hash value as formatted RawByteString /// /// /// Optional parameter. If a formatting class is being passed the formatting /// will be applied to the returned string. Otherwise no formatting is /// being used. /// /// /// Hash value of the last performed hash calculation /// /// /// We recommend to use a formatting which results in 7 bit ASCII chars /// being returned, otherwise the conversion into the RawByteString might /// result in strange characters in the returned result. /// function DigestAsRawByteString(Format: TDECFormatClass = nil): RawByteString; /// /// Calculates the hash value (digest) for a given buffer /// /// /// Untyped buffer the hash shall be calculated for /// /// /// Size of the buffer in byte /// /// /// Byte array with the calculated hash value /// function CalcBuffer(const Buffer; BufferSize: Integer): TBytes; /// /// Calculates the hash value (digest) for a given buffer /// /// /// The TBytes array the hash shall be calculated on /// /// /// Byte array with the calculated hash value /// function CalcBytes(const Data: TBytes): TBytes; /// /// Calculates the hash value (digest) for a given unicode string /// /// /// The string the hash shall be calculated on /// /// /// Formatting class from DECFormat. The formatting will be applied to the /// returned digest value. This parameter is optional. /// /// /// string with the calculated hash value /// function CalcString(const Value: string; Format: TDECFormatClass = nil): string; overload; /// /// Calculates the hash value (digest) for a given rawbytestring /// /// /// The string the hash shall be calculated on /// /// /// Formatting class from DECFormat. The formatting will be applied to the /// returned digest value. This parameter is optional. /// /// /// string with the calculated hash value /// function CalcString(const Value: RawByteString; Format: TDECFormatClass): RawByteString; overload; /// /// Calculates the hash value over a givens stream of bytes /// /// /// Memory or file stream over which the hash value shall be calculated. /// The stream must be assigned. The hash value will always be calculated /// from the current position of the stream. /// /// /// Number of bytes within the stream over which to calculate the hash value /// /// /// In this byte array the calculated hash value will be returned /// /// /// Optional callback routine. It can be used to display the progress of /// the operation. /// procedure CalcStream(const Stream: TStream; Size: Int64; var HashResult: TBytes; const OnProgress:TDECProgressEvent = nil); overload; /// /// Calculates the hash value over a givens stream of bytes /// /// /// Memory or file stream over which the hash value shall be calculated. /// The stream must be assigned. The hash value will always be calculated /// from the current position of the stream. /// /// /// Number of bytes within the stream over which to calculate the hash value /// /// /// Optional formatting class. The formatting of that will be applied to /// the returned hash value. /// /// /// Optional callback routine. It can be used to display the progress of /// the operation. /// /// /// Hash value over the bytes in the stream, formatted with the formatting /// passed as format parameter, if used. /// function CalcStream(const Stream: TStream; Size: Int64; Format: TDECFormatClass = nil; const OnProgress:TDECProgressEvent = nil): RawByteString; overload; /// /// Calculates the hash value over the contents of a given file /// /// /// Path and name of the file to be processed /// /// /// Here the resulting hash value is being returned as byte array /// /// /// Optional callback. If being used the hash calculation will call it from /// time to time to return the current progress of the operation /// procedure CalcFile(const FileName: string; var HashResult: TBytes; const OnProgress:TDECProgressEvent = nil); overload; /// /// Calculates the hash value over the contents of a given file /// /// /// Path and name of the file to be processed /// /// /// Optional parameter: Formatting class. If being used the formatting is /// being applied to the returned string with the calculated hash value /// /// /// Optional callback. If being used the hash calculation will call it from /// time to time to return the current progress of the operation /// /// /// Calculated hash value as RawByteString. /// /// /// We recommend to use a formatting which results in 7 bit ASCII chars /// being returned, otherwise the conversion into the RawByteString might /// result in strange characters in the returned result. /// function CalcFile(const FileName: string; Format: TDECFormatClass = nil; const OnProgress:TDECProgressEvent = nil): RawByteString; overload; /// /// Returns the current value of the padding byte used to fill up data /// if necessary /// function GetPaddingByte: Byte; /// /// Changes the value of the padding byte used to fill up data /// if necessary /// /// /// New value for the padding byte /// procedure SetPaddingByte(Value: Byte); /// /// Defines the byte used in the KDF methods to padd the end of the data /// if the length of the data cannot be divided by required size for the /// hash algorithm without reminder /// property PaddingByte: Byte read GetPaddingByte write SetPaddingByte; end; /// /// Interface for all hash classes which are able to operate on bit sized /// message lengths instead of byte sized ones only. /// IDECHashBitsized = Interface(IDECHash) /// /// Returns the number of bits the final byte of the message consists of /// function GetFinalByteLength: UInt8; /// /// Sets the number of bits the final byte of the message consists of /// procedure SetFinalByteLength(const Value: UInt8); /// /// Setting this to a number of bits allows to process messages which have /// a length which is not a exact multiple of bytes. /// property FinalBitLength : UInt8 read GetFinalByteLength write SetFinalByteLength; end; implementation end.