0

I have the following Java routine that I need to translate to Delphi. I know how to do the Base64 encoding but I am not sure what the Delphi equivalent for MessageDigest is. How would I write this method in Delphi?

    private String getHashCode(String s) {
        String outputHash;
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1"); // SHA-256
            byte[] hash = digest.digest(s.getBytes(StandardCharsets.UTF_8));
            outputHash = Base64.getEncoder().encodeToString(hash);
        } catch (Exception ex) {
            outputHash = "";
        }
        return outputHash;
    }
Johan
  • 317
  • 3
  • 11
  • Hashing is about bytes, not texts. Make sure your Delphi `String` really has an UTF-8 encoding, otherwise hash sums will differ. – AmigoJack Oct 26 '22 at 09:36
  • function TfrmMain.GetXMLHashCode(pString: string): string; var SHA1: TIdHashSHA1; Hash: TidBytes; begin SHA1 := TIdHashSHA1.Create; try try Hash := SHA1.HashString(pString); Result := TNetEncoding.Base64.EncodeBytesToString(Hash); except on E: Exception do Result := ''; end; finally SHA1.Free; end; end; – Johan Oct 27 '22 at 08:50

0 Answers0