我在PHP轉換代碼德爾福
function wa_pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false)
{
$algorithm = strtolower($algorithm);
if (! in_array($algorithm, hash_algos(), true)) {
die('PBKDF2 ERROR: Invalid hash algorithm.');
}
if ($count <= 0 || $key_length <= 0) {
die('PBKDF2 ERROR: Invalid parameters.');
}
$hash_length = strlen(hash($algorithm, "", true));
$block_count = ceil($key_length/$hash_length);
$output = "";
for ($i = 1; $i <= $block_count; $i++) {
$last = $salt . pack("N", $i);
$last = $xorsum = hash_hmac($algorithm, $last, $password, true);
for ($j = 1; $j < $count; $j++) {
$xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
}
$output .= $xorsum;
}
if ($raw_output) {
return substr($output, 0, $key_length);
} else {
return bin2hex(substr($output, 0, $key_length));
}
}
此代碼,我想搬到這個代碼德爾福,我做到了,到目前爲止
function wa_pbkdf2(password,salt:ansistring;count,keylength:integer;outputraw:boolean=false):ansistring;
var
hashlength:integer;
blockcount:integer;
last:ansistring;
xorsum:ansistring;
I: Integer;
j: Integer;
begin
hashlength:=Length(THMACUtils<TIdHMACSHA1>.HMAC('',''));
if frac(keylength/hashlength)>0.5 then //VOLTAAQUI
blockcount:=round(keylength/hashlength)
else
blockcount:=Round(keylength/hashlength)+1;
for I := 1 to blockcount do
begin
last:=salt+ReturnINT32(i);
xorsum:=StringOf(THMACUtils<TIdHMACSHA1>.HMAC(password,last));
last:=xorsum;
res:=last;
for j := 1 to count-1 do
begin
end;
end;
end;
我怎樣才能做到這一點。
我的問題是在這裏
$xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
我知道^= is XOR
,但我不知道如何將這個代碼轉換爲德爾福。
請停止使用AnsiString類型來保存二進制數據。字符串用於文本。使用一個字節數組。 –