The Indy
function BytesToString
is giving me funny results, i.e. not the one I am expecting.
Here is my code;
function String2Hex(S: String): String;
var I: Integer;
begin
Result:= '';
for I := 1 to length (S) do
Result:= Result+IntToHex(ord(S[i]),2);
end;
function SHA1FromString(AString:AnsiString): TidBytes;
var
SHA1: TIdHashSHA1;
begin
SHA1 := TIdHashSHA1.Create;
try
//Result := SHA1.HashBytes(AString);
Result := SHA1.HashString(AString, IndyTextEncoding_UTF8)
// SHA1.HashStringAsHex(UTF8Encode(AString), IndyTextEncoding_UTF8);
finally
SHA1.Free;
end;
end;
function bintoAscii(bin: array of byte): AnsiString;
var i: integer;
begin
SetLength(Result, Length(bin));
for i := 0 to Length(bin)-1 do
Result[1+i] := AnsiChar(bin[i]);
end;
function HashSamsung(passcode: String; salt: Int64):AnsiString;
var
salted_pass : AnsiString;
g_digest:AnsiString;
buf: TidBytes;
I: Integer;
step: tIDBytes;
step2: AnsiString;
salt_hex: AnsiString;
a: AnsiString;
begin
salt_Hex := LowerCase(IntToHex(salt, 2));
salted_pass := passcode + salt_hex;
buf := nil;
step := nil;
for I := 0 to 1023 do
begin
step2 := BytesToString(buf,IndyTextEncoding_UTF8) + IntToStr(i) + salted_pass;
buf := SHA1FromString(step2);
showmessage(String2Hex(BytesToString(buf,IndyTextEncoding_UTF8)));
showmessage(String2Hex(bintoAscii(buf)));
end;
// showmessage(String2Hex(bintoAscii(buf) ));
// Result := String2Hex(bintoAscii(buf));
//Result := buf;
end;
The function is called as so;
HashSamsung('0000', 988796901418269782);
Now, when the code reaches the ShowMessage
calls, the results are different on both message boxes. The output from the BytesToString
message box is;
FFFD2EFFFD0.....
and the output from the bintoascii
message box is;
ab2ec50e0f.....
The second result is the one I am expecting. So my question is, why is the BytesToString
function giving a different result to the bintoascii
function?