Ru-Board.club
← Вернуться в раздел «Прикладное программирование»

» Реализация функции crypt() в Delphi

Автор: Tropin
Дата сообщения: 25.12.2003 11:14
как мне в дельфи реализовать функцию crypt() аналогично системной *nix'овой???

hash:=crypt(pass, salt);
Автор: Delphist
Дата сообщения: 25.12.2003 11:32
Хы ну ты сказанул. Кто ее тебе сделает...
А если реально вот реализация SHA переделана из Miracl.
unit SHA;

interface

const
H0 = $6a09e667f3bcc908;
H1 = $bb67ae8584caa73b;
H2 = $3c6ef372fe94f82b;
H3 = $a54ff53a5f1d36f1;
H4 = $510e527fade682d1;
H5 = $9b05688c2b3e6c1f;
H6 = $1f83d9abfb41bd6b;
H7 = $5be0cd19137e2179;
H8 = $cbbb9d5dc1059ed8;
H9 = $629a292a367cd507;
HA = $9159015a3070dd17;
HB = $152fecd8f70e5939;
HC = $67332667ffc00b31;
HD = $8eb44a8768581511;
HE = $db0c2e0d64f98fa7;
HF = $47b5481dbefa4fa4;

PAD = $80;
ZERO = 0;

K : array[0..79] of Int64 =($428a2f98d728ae22,$7137449123ef65cd,$b5c0fbcfec4d3b2f,$e9b5dba58189dbbc,
$3956c25bf348b538,$59f111f1b605d019,$923f82a4af194f9b,$ab1c5ed5da6d8118,
$d807aa98a3030242,$12835b0145706fbe,$243185be4ee4b28c,$550c7dc3d5ffb4e2,
$72be5d74f27b896f,$80deb1fe3b1696b1,$9bdc06a725c71235,$c19bf174cf692694,
$e49b69c19ef14ad2,$efbe4786384f25e3,$0fc19dc68b8cd5b5,$240ca1cc77ac9c65,
$2de92c6f592b0275,$4a7484aa6ea6e483,$5cb0a9dcbd41fbd4,$76f988da831153b5,
$983e5152ee66dfab,$a831c66d2db43210,$b00327c898fb213f,$bf597fc7beef0ee4,
$c6e00bf33da88fc2,$d5a79147930aa725,$06ca6351e003826f,$142929670a0e6e70,
$27b70a8546d22ffc,$2e1b21385c26c926,$4d2c6dfc5ac42aed,$53380d139d95b3df,
$650a73548baf63de,$766a0abb3c77b2a8,$81c2c92e47edaee6,$92722c851482353b,
$a2bfe8a14cf10364,$a81a664bbc423001,$c24b8b70d0f89791,$c76c51a30654be30,
$d192e819d6ef5218,$d69906245565a910,$f40e35855771202a,$106aa07032bbd1b8,
$19a4c116b8d2d0c8,$1e376c085141ab53,$2748774cdf8eeb99,$34b0bcb5e19b48a8,
$391c0cb3c5c95a63,$4ed8aa4ae3418acb,$5b9cca4f7763e373,$682e6ff3d6b2b8a3,
$748f82ee5defb2fc,$78a5636f43172f60,$84c87814a1f0ab72,$8cc702081a6439ec,
$90befffa23631e28,$a4506cebde82bde9,$bef9a3f7b2c67915,$c67178f2e372532b,
$ca273eceea26619c,$d186b8c721c0c207,$eada7dd6cde0eb1e,$f57d4f7fee6ed178,
$06f067aa72176fba,$0a637dc5a2c898a6,$113f9804bef90dae,$1b710b35131c471b,
$28db77f523047d84,$32caab7b40c72493,$3c9ebe0a15c9bebc,$431d67c49c100d4c,
$4cc5d4becb3e42b6,$597f299cfc657e2a,$5fcb6fab3ad6faec,$6c44198c4a475817);

type
DWORD = LongWord;

//* secure hash Algorithm structure */

TSHA512 = packed record
length : array [0..1] of Int64;
h : array [0..7] of Int64;
w : array [0..79] of Int64;
end;

TSHA384 = TSHA512;
TSHA = TSHA512;

procedure shs512_init(var sh : TSHA512);
procedure shs384_init(var sh : TSHA384);
procedure shs512_process(var sh : TSHA512; byte : Byte);
procedure shs384_process(var sh : TSHA384; byte : Byte);
procedure shs512_hash(var sh : TSHA512; var hash : array of Char);//hash 64 byte
procedure shs384_hash(var sh : TSHA384; var hash : array of Char); //hash 48 Byte

implementation

function S(n, x : Int64) : Int64;
begin
Result := (x SHR n) OR (x SHL (64 - n));
end;

function R(n, x : Int64) : Int64;
begin
Result := (x SHR n);
end;

function Ch(x, y, z : Int64) : Int64;
begin
Result := (x AND y) XOR (NOT (x) AND z);
end;

function Maj(x, y, z : Int64) : Int64;
begin
Result := (x AND y) XOR (x AND z) XOR (y AND z);
end;

function Sig0(x : Int64) : Int64;
begin
Result := S(28, x) XOR S(34, x) XOR S(39, x);
end;

function Sig1(x : Int64) : Int64;
begin
Result := S(14, x) XOR S(18, x) XOR S(41, x);
end;

function theta0(x : Int64) : Int64;
begin
Result := S(1, x) XOR S(8, x) XOR R(7, x);
end;

function theta1(x : Int64) : Int64;
begin
Result := S(19, x) XOR S(61, x) XOR R(6, x);
end;

procedure shs_transform(var sh : TSHA);
var a,b,c,d,e,f,g,h,t1,t2 : Int64;
j : Integer;
begin
//* basic transformation step */
for j := 16 to 79 do
sh.w[j] := theta1(sh.w[j-2]) + sh.w[j-7] + theta0(sh.w[j-15]) + sh.w[j-16];
a := sh.h[0]; b := sh.h[1]; c := sh.h[2]; d := sh.h[3];
e := sh.h[4]; f := sh.h[5]; g := sh.h[6]; h := sh.h[7];
for j := 0 to 79 do begin
//* 80 times - mush it up */
t1 := h+Sig1(e)+Ch(e,f,g)+K[j]+sh.w[j];
t2 := Sig0(a) + Maj(a,b,c);
h := g; g := f; f := e;
e := d+t1;
d := c; c := b; b := a;
a := t1+t2;
end;
sh.h[0] := sh.h[0]+a; sh.h[1] := sh.h[1]+b; sh.h[2] := sh.h[2]+c; sh.h[3] := sh.h[3]+d;
sh.h[4] := sh.h[4]+e; sh.h[5] := sh.h[5]+f; sh.h[6] := sh.h[6]+g; sh.h[7] := sh.h[7]+h;
end;

procedure shs512_init(var sh : TSHA512);
var i : Integer;
begin
//* re-initialise */
for i := 0 to 79 do
sh.w[i] := 0;
sh.length[0] := 0;
sh.length[1] := 0;
sh.h[0] := H0;
sh.h[1] := H1;
sh.h[2] := H2;
sh.h[3] := H3;
sh.h[4] := H4;
sh.h[5] := H5;
sh.h[6] := H6;
sh.h[7] := H7;
end;

procedure shs384_init(var sh : TSHA384);
var i : Integer;
begin
//* re-initialise */
for i := 0 to 79 do
sh.w[i] := 0;
sh.length[0] := 0;
sh.length[1] := 0;
sh.h[0] := H8;
sh.h[1] := H9;
sh.h[2] := HA;
sh.h[3] := HB;
sh.h[4] := HC;
sh.h[5] := HD;
sh.h[6] := HE;
sh.h[7] := HF;
end;

procedure shs512_process(var sh : TSHA512; byte : Byte);
var cnt : Integer;
begin
//* process the next message byte */
cnt := (sh.length[0] div 64) mod 16;
sh.w[cnt] := sh.w[cnt] SHL 8;
sh.w[cnt] := sh.w[cnt] OR (byte AND $FF);
sh.length[0] := sh.length[0] + 8;
if (sh.length[0] = 0) then begin
Inc(sh.length[1]);
sh.length[0] := 0;
end;
if (sh.length[0] mod 1024) = 0 then shs_transform(sh);
end;

procedure shs384_process(var sh : TSHA384; byte : Byte);
var cnt : Integer;
begin
//* process the next message byte */
cnt := (sh.length[0] div 64) mod 16;
sh.w[cnt] := sh.w[cnt] SHL 8;
sh.w[cnt] := sh.w[cnt] OR (byte AND $FF);
sh.length[0] := sh.length[0] + 8;
if sh.length[0] = 0 then begin
Inc(sh.length[1]);
sh.length[0] := 0;
end;
if (sh.length[0] mod 1024) = 0 then shs_transform(sh);
end;

procedure shs512_hash(var sh : TSHA512; var hash : array of Char);//hash 64 byte
var i : Integer;
len0, len1 : Int64;
begin
//* pad message and finish - supply digest */
len0 := sh.length[0];
len1 := sh.length[1];
shs512_process(sh, PAD);
while (sh.length[0] mod 1024) <> 896 do
shs512_process(sh, ZERO);
sh.w[14] := len1;
sh.w[15] := len0;
shs_transform(sh);
for i := 0 to 63 do
//* convert to bytes */
hash[i] := Char(Byte(sh.h[i div 8] SHR (8*(7 - i mod 8))) AND $0ff);
shs512_init(sh);
end;

procedure shs384_hash(var sh : TSHA384; var hash : array of Char); //hash 48 Byte
var i : Integer;
len0, len1 : Int64;
begin
//* pad message and finish - supply digest */
len0 := sh.length[0];
len1 := sh.length[1];
shs512_process(sh, PAD);
while (sh.length[0] mod 1024) <> 896 do
shs384_process(sh, ZERO);
sh.w[14] := len1;
sh.w[15] := len0;
shs_transform(sh);
for i := 0 to 47 do
//* convert to bytes */
hash[i] := Char(Byte(sh.h[i div 8] SHR (8*(7-i mod 8))) AND $0ff);
shs384_init(sh);
end;

end.

вот и пример использования

procedure TForm1.BitBtn4Click(Sender: TObject);
const test = 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu';
var i : Integer;
hash : array[0..63] of Char;
sh512 : TSHA512;
s : String;
begin
mm.Lines.Clear;
mm.Lines.Add(test);
shs512_init(sh512);
for i:=1 to Length(test) do
shs512_process(sh512, Byte(test[i]));
shs512_hash(sh512, hash);
s := '';
for i:=0 to 63 do
s := s + IntToHex(Byte(hash[i]), 2);
mm.Lines.Add('64 bytes hash - ' + s);
shs384_init(sh512);
for i:=1 to Length(test) do
shs384_process(sh512, Byte(test[i]));
shs384_hash(sh512, hash);
s := '';
for i:=0 to 47 do
s := s + IntToHex(Byte(hash[i]), 2);
mm.Lines.Add('48 bytes hash - ' + s);
end;

Автор: Tropin
Дата сообщения: 25.12.2003 12:48
Delphist
Ну не фига себе!!!
Спасибо, буду разбираться...
Ответь, почему нельзя найти реализацию именно функции crypt???
мне нужно связать две программы на Delphi и Perl, так вот в Per эта функция уже есть...
А так мне придется писать две своих...

Страницы: 1

Предыдущая тема: "Детский" вопрос по поп-меню.


Форум Ru-Board.club — поднят 15-09-2016 числа. Цель - сохранить наследие старого Ru-Board, истории становления российского интернета. Сделано для людей.