Added Sha1,224,256,384,512

This commit is contained in:
R. Eric Wheeler 2017-02-27 21:10:14 -08:00
parent 98a724fef3
commit 99e482b8bc
14 changed files with 498 additions and 16 deletions

View File

@ -1,38 +1,72 @@
{*******************************************************}
{ Renegade BBS }
{ Copyright (c) 1990-2013 The Renegade Dev Team }
{ Copyleft (ↄ) 2016-2017 Renegade BBS }
{ This file is part of Renegade BBS }
{ Renegade is free software: you can redistribute it }
{ and/or modify it under the terms of the GNU General }
{ Public License as published by the Free Software }
{ Foundation, either version 3 of the License, or }
{ (at your option) any later version. }
{ Renegade 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 General Public }
{ License for more details. }
{ You should have received a copy of the GNU General }
{ Public License along with Renegade. If not, see }
{ <http://www.gnu.org/licenses/>. }
{*******************************************************}
{ _______ __ }
{ | _ .-----.-----.-----.-----.---.-.--| .-----. }
{ |. l | -__| | -__| _ | _ | _ | -__| }
{ |. _ |_____|__|__|_____|___ |___._|_____|_____| }
{ |: | | |_____| }
{ |::.|:. | }
{ `--- ---' }
{*******************************************************}
{$codepage utf8} {$codepage utf8}
{$h+} {$h+}
{$mode objfpc} {$mode objfpc}
Unit Hash.Md; unit Hash.Md;
interface interface
Uses uses
md5, md5,
SysUtils, SysUtils,
Classes; Classes;
Type type
Md = class(TObject) Md = class(TObject)
public public
function md2(S : UTF8String) : AnsiString; function md2(S: UTF8String): ansistring;
function md4(S : UTF8String) : AnsiString; function md4(S: UTF8String): ansistring;
function md5(S : UTF8STring) : AnsiString; function md5(S: UTF8STring): ansistring;
end; end;
implementation implementation
function Md.md2(S : UTF8String) : AnsiString; function Md.md2(S: UTF8String): ansistring;
begin begin
Result := MD2Print(MDString(S, MD_VERSION_2)); Result := MD2Print(MDString(S, MD_VERSION_2));
end; end;
function Md.md4(S : UTF8String) : AnsiString; function Md.md4(S: UTF8String): ansistring;
begin begin
Result := MD4Print(MDString(S, MD_VERSION_4)); Result := MD4Print(MDString(S, MD_VERSION_4));
end; end;
function Md.md5(S : UTF8String) : AnsiString; function Md.md5(S: UTF8String): ansistring;
begin begin
Result := MD5Print(MDString(S, MD_VERSION_5)); Result := MD5Print(MDString(S, MD_VERSION_5));
end; end;
End. end.

View File

@ -0,0 +1,302 @@
{*******************************************************}
{ Renegade BBS }
{ Copyright (c) 1990-2013 The Renegade Dev Team }
{ Copyleft (ↄ) 2016-2017 Renegade BBS }
{ This file is part of Renegade BBS }
{ Renegade is free software: you can redistribute it }
{ and/or modify it under the terms of the GNU General }
{ Public License as published by the Free Software }
{ Foundation, either version 3 of the License, or }
{ (at your option) any later version. }
{ Renegade 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 General Public }
{ License for more details. }
{ You should have received a copy of the GNU General }
{ Public License along with Renegade. If not, see }
{ <http://www.gnu.org/licenses/>. }
{*******************************************************}
{ _______ __ }
{ | _ .-----.-----.-----.-----.---.-.--| .-----. }
{ |. l | -__| | -__| _ | _ | _ | -__| }
{ |. _ |_____|__|__|_____|___ |___._|_____|_____| }
{ |: | | |_____| }
{ |::.|:. | }
{ `--- ---' }
{*******************************************************}
{$mode objfpc}
{$codepage utf8}
{$h+}
unit Hash.Sha;
interface
uses
SysUtils,
Classes,
OpenSSL,
CTypes;
const
SHA512_DIGEST_LENGTH = 64;
SHA384_DIGEST_LENGTH = 48;
SHA256_DIGEST_LENGTH = 32;
SHA224_DIGEST_LENGTH = 28;
type
RTSha = class(TObject)
public
constructor Create();
destructor Destroy();override;
function Sha1(S: UTF8String): ansistring;
function Sha224(S: UTF8String): ansistring;
function Sha256(S: UTF8String): ansistring;
function Sha384(S: UTF8String): ansistring;
function Sha512(S: UTF8String): ansistring;
end;
implementation
constructor RTSha.Create();
var
HaveOpenSSL : Boolean;
begin
HaveOpenSSL := InitSSLInterface;
if not HaveOpenSSL then
begin
raise Exception.Create('Please install OpenSSL.') at
get_caller_addr(get_frame),
get_caller_frame(get_frame);
end;
end;
destructor RTSha.Destroy();
begin
inherited Destroy;
EVPcleanup;
DestroySSLInterface;
end;
function RTSha.Sha1(S: UTF8String): ansistring;
var
Digest : PEVP_MD;
ShaCTX : PEVP_MD_CTX;
HexValue : AnsiString;
BinValue : PChar;
Hash : PByte;
DigestLength: pcuint;
begin
GetMem(Hash, SHA_DIGEST_LENGTH);
GetMem(DigestLength, SHA_DIGEST_LENGTH);
GetMem(ShaCTX, SizeOf(PEVP_MD_CTX));
GetMem(BinValue, SHA_DIGEST_LENGTH*2);
SetLength(HexValue, SHA_DIGEST_LENGTH*2);
try
Digest := EvpGetDigestByName('sha1');
EVP_DigestInit(ShaCTX, Digest);
EVP_DigestUpdate(ShaCTX, @S[1], Length(S));
EVP_DigestFinal(ShaCTX, Hash, DigestLength);
except
On e: Exception do
begin
WriteLn(e.Message);
Writeln(e.HelpContext);
Free;
exit;
end;
end;
Move(Hash[0], BinValue[0], SHA_DIGEST_LENGTH);
BinToHex(BinValue, PChar(HexValue), SHA_DIGEST_LENGTH);
// Cleanup
FreeMem(Hash);
FreeMem(DigestLength);
FreeMem(BinValue);
Result := LowerCase(HexValue);
end;
function RTSha.Sha224(S: UTF8String): ansistring;
var
Digest : PEVP_MD;
ShaCTX : PEVP_MD_CTX;
HexValue : AnsiString;
BinValue : PChar;
Hash : PByte;
DigestLength: pcuint;
begin
GetMem(Hash, SHA224_DIGEST_LENGTH);
GetMem(DigestLength, SHA224_DIGEST_LENGTH);
GetMem(ShaCTX, SizeOf(PEVP_MD_CTX));
GetMem(BinValue, SHA224_DIGEST_LENGTH);
SetLength(HexValue, SHA224_DIGEST_LENGTH*2);
try
Digest := EvpGetDigestByName('sha224');
EVP_DigestInit(ShaCTX, Digest);
EVP_DigestUpdate(ShaCTX, @S[1], Length(S));
EVP_DigestFinal(ShaCTX, Hash, DigestLength);
except
On e: Exception do
begin
WriteLn(e.Message);
Writeln(e.HelpContext);
Free;
exit;
end;
end;
Move(Hash[0], BinValue[0], SHA224_DIGEST_LENGTH);
BinToHex(BinValue, PChar(HexValue), SHA224_DIGEST_LENGTH);
// Cleanup
FreeMem(Hash);
FreeMem(DigestLength);
FreeMem(BinValue);
Result := LowerCase(HexValue);
end;
function RTSha.Sha256(S: UTF8String): ansistring;
var
Digest : PEVP_MD;
ShaCTX : PEVP_MD_CTX;
HexValue : AnsiString;
BinValue : PChar;
Hash : PByte;
DigestLength: pcuint;
begin
GetMem(Hash, SHA256_DIGEST_LENGTH);
GetMem(DigestLength, SHA256_DIGEST_LENGTH);
GetMem(ShaCTX, SizeOf(PEVP_MD_CTX));
GetMem(BinValue, SHA256_DIGEST_LENGTH);
SetLength(HexValue, SHA256_DIGEST_LENGTH*2);
try
Digest := EvpGetDigestByName('sha256');
EVP_DigestInit(ShaCTX, Digest);
EVP_DigestUpdate(ShaCTX, @S[1], Length(S));
EVP_DigestFinal(ShaCTX, Hash, DigestLength);
except
On e: Exception do
begin
WriteLn(e.Message);
Writeln(e.HelpContext);
Free;
exit;
end;
end;
Move(Hash[0], BinValue[0], SHA256_DIGEST_LENGTH);
BinToHex(BinValue, PChar(HexValue), SHA256_DIGEST_LENGTH);
// Cleanup
FreeMem(Hash);
FreeMem(DigestLength);
FreeMem(BinValue);
Result := LowerCase(HexValue);
end;
function RTSha.Sha384(S: UTF8String): ansistring;
var
Digest : PEVP_MD;
ShaCTX : PEVP_MD_CTX;
HexValue : AnsiString;
BinValue : PChar;
Hash : PByte;
DigestLength: pcuint;
begin
GetMem(Hash, SHA384_DIGEST_LENGTH);
GetMem(DigestLength, SHA384_DIGEST_LENGTH);
GetMem(ShaCTX, SizeOf(PEVP_MD_CTX));
GetMem(BinValue, SHA384_DIGEST_LENGTH);
SetLength(HexValue, SHA384_DIGEST_LENGTH*2);
try
Digest := EvpGetDigestByName('sha384');
EVP_DigestInit(ShaCTX, Digest);
EVP_DigestUpdate(ShaCTX, @S[1], Length(S));
EVP_DigestFinal(ShaCTX, Hash, DigestLength);
except
On e: Exception do
begin
WriteLn(e.Message);
Writeln(e.HelpContext);
Free;
exit;
end;
end;
Move(Hash[0], BinValue[0], SHA384_DIGEST_LENGTH);
BinToHex(BinValue, PChar(HexValue), SHA384_DIGEST_LENGTH);
// Cleanup
FreeMem(Hash);
FreeMem(DigestLength);
FreeMem(BinValue);
Result := LowerCase(HexValue);
end;
function RTSha.Sha512(S: UTF8String): ansistring;
var
Digest : PEVP_MD;
ShaCTX : PEVP_MD_CTX;
HexValue : AnsiString;
BinValue : PChar;
Hash : PByte;
DigestLength: pcuint;
begin
GetMem(Hash, SHA512_DIGEST_LENGTH);
GetMem(DigestLength, SHA512_DIGEST_LENGTH);
GetMem(ShaCTX, SizeOf(PEVP_MD_CTX));
GetMem(BinValue, SHA512_DIGEST_LENGTH);
SetLength(HexValue, SHA512_DIGEST_LENGTH*2);
try
Digest := EvpGetDigestByName('sha512');
EVP_DigestInit(ShaCTX, Digest);
EVP_DigestUpdate(ShaCTX, @S[1], Length(S));
EVP_DigestFinal(ShaCTX, Hash, DigestLength);
except
On e: Exception do
begin
WriteLn(e.Message);
Writeln(e.HelpContext);
Free;
exit;
end;
end;
Move(Hash[0], BinValue[0], SHA512_DIGEST_LENGTH);
BinToHex(BinValue, PChar(HexValue), SHA512_DIGEST_LENGTH);
// Cleanup
FreeMem(Hash);
FreeMem(DigestLength);
FreeMem(BinValue);
Result := LowerCase(HexValue);
end;
end.

View File

View File

View File

View File

View File

View File

@ -0,0 +1,22 @@
{$codepage utf8}
{$h+}{$mode objfpc}
Program ShaTest;
Uses
SysUtils,
Classes,
Hash.Sha;
var
Hash : RTSha;
S : UTF8String;
begin
Hash := RTSha.Create;
S := 'testing';
WriteLn(Hash.Sha1(S));
Writeln(Hash.Sha224(S));
Writeln(Hash.Sha256(S));
Writeln(Hash.Sha384(S));
Writeln(Hash.Sha512(S));
Hash.Free;
Exit;
end.

View File

@ -0,0 +1,24 @@
<?php
$algos = [
'sha1',
'sha224',
'sha256',
'sha384',
'sha512',
];
$testWord = 'testing';
$result = [];
foreach($algos as $algo)
{
$hash = hash($algo, $testWord);
$result[$algo] = [
'sha' => (int) trim(filter_var($algo, FILTER_SANITIZE_NUMBER_INT), '+-'),
'length' => (int)strlen($hash),
'word' => $testWord,
'hash' => $hash,
];
}
file_put_contents('sha_hashes.json', json_encode($result, JSON_PRETTY_PRINT));

23
tests/Makefile.fpc Normal file
View File

@ -0,0 +1,23 @@
#
# Makefile.fpc for hash units example
#
[target]
programs=MDTest GeneratePascalShaHashes
[require]
packages=
[install]
fpcpackage=y
[compiler]
options=-Fu..
[default]
fpcdir=${FPCDIR}

22
tests/ShaTest.pp Normal file
View File

@ -0,0 +1,22 @@
{$codepage utf8}
{$h+}{$mode objfpc}
Program ShaTest;
Uses
SysUtils,
Classes,
Hash.Sha;
var
Hash : RTSha;
S : UTF8String;
begin
Hash := RTSha.Create;
S := 'testing';
WriteLn(Hash.Sha1(S));
Writeln(Hash.Sha224(S));
Writeln(Hash.Sha256(S));
Writeln(Hash.Sha384(S));
Writeln(Hash.Sha512(S));
Hash.Free;
Exit;
end.

23
tests/autogen.sh Executable file
View File

@ -0,0 +1,23 @@
#!/bin/bash
fpcBaseInput=$1
if [ "$fpcBaseInput" == "" ]; then
fpcBase=$(dirname $(dirname $(which fpc)))/lib/fpc/$(fpc -iV)
else
fpcBase=$fpcBaseInput
fi
if [ ! -d $fpcBase ]; then
echo "Need an FPC base to fpc sources"
echo "Usually /usr/lib{32,64}/fpc/{fpc-version}/ on Linux machines."
echo "or C:\FPC\..\.. on Windows machines."
exit 255
fi
[ -f "Makefile" ] && rm Makefile
echo "Making with unit/source directory '$fpcBase'"
FPCDIR="$fpcBase" fpcmake

32
tests/sha_hashes.json Normal file
View File

@ -0,0 +1,32 @@
{
"sha1": {
"sha": 1,
"length": 40,
"word": "testing",
"hash": "dc724af18fbdd4e59189f5fe768a5f8311527050"
},
"sha224": {
"sha": 224,
"length": 56,
"word": "testing",
"hash": "9e8965af89bd98c015512f1eb17ae07f48494da2d9a06a8f9659f749"
},
"sha256": {
"sha": 256,
"length": 64,
"word": "testing",
"hash": "cf80cd8aed482d5d1527d7dc72fceff84e6326592848447d2dc0b0e87dfc9a90"
},
"sha384": {
"sha": 384,
"length": 96,
"word": "testing",
"hash": "cf4811d74fd40504674fc3273f824fa42f755b9660a2e902b57f1df74873db1a91a037bcee65f1a88ecd1ef57ff254c9"
},
"sha512": {
"sha": 512,
"length": 128,
"word": "testing",
"hash": "521b9ccefbcd14d179e7a1bb877752870a6d620938b28a66a107eac6e6805b9d0989f45b5730508041aa5e710847d439ea74cd312c9355f1f2dae08d40e41d50"
}
}