0

I use the below Java snippet to generate a hash password using an input and salt. Is there a way I can get that in Linux command line?

public String getPwd(String input, String salt) {
    MessageDigest md = MessageDigest.getInstance("SHA-512");
    md.update(salt.getBytes(StandardCharsets.UTF_8));
    byte[] pwd= md.digest(input.getBytes(StandardCharsets.UTF_8));
    System.out.println(Base64.encodeBase64URLSafeString(pwd));
}
Sergei Voitovich
  • 2,804
  • 3
  • 25
  • 33
Guru
  • 16,456
  • 2
  • 33
  • 46

1 Answers1

1

You can use sha512sum.

cat salt.dat myfile.dat | sha512sum -b

Seems the -b is required for binary input (defaults to text, which can potentially result in different checksums depending on the encoding).

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • Does that myfile.dat contain my first input? – Guru Sep 16 '19 at 12:05
  • @Guru if you're looking to get the same result as from your Java code (except the Base64 part), then yes. It needs to be your input in UTF-8. – Kayaman Sep 16 '19 at 12:12
  • I am not getting the result same as the Java code. Yes, my input is a normal string. Not sure what am I doing wrong – Guru Sep 16 '19 at 13:07
  • 1
    There's no base64 encoding in the commandline, it provides the result as a hexstring. If you're just trying to verify your code, print out the [hexstring](https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java). – Kayaman Sep 16 '19 at 13:11
  • @Guru just try `cat salt.dat myfile.dat | sha512sum -b | base64` – Sergei Voitovich Sep 16 '19 at 13:12
  • @SergeyVoytovich that'll base64 encode the hexstring and not the bytes, so it won't give the same result. – Kayaman Sep 16 '19 at 13:14
  • Thanks @Kayaman .. i was hoping I will get result in command line along with base64 encoding.. – Guru Sep 16 '19 at 13:40