Ram
Ram

Reputation: 127

What is the java equivalent for the SHA1 hash that is produced in .Net?

Am trying to authenticate a soap webservice, but the SHA1 hash that I produce in java is not working but the hash produced with .Net works.

What is the java equivalent for this .Net code?

//.Net

var token = "H?OIgSJ35~LKJ:9~~7&sUtHDeKAv*O@is?cEwV[}!i@u%}";
var shaProvider = new SHA1Managed();
var rawKey = Encoding.Unicode.GetBytes(token);
var rawHash = shaProvider.ComputeHash(rawKey);
var signature = BitConverter.ToString(rawHash).Replace("-", "").ToLower();

Hash produced:a508a29efeea2821e519fcbf64f164dd5d672233

//Java - This is what I tried using commons-codec-1.4.jar

String token = "H?OIgSJ35~LKJ:9~~7&sUtHDeKAv*O@is?cEwV[}!i@u%}";
MessageDigest cript = null;
try {
    cript = MessageDigest.getInstance("SHA1");
} catch (NoSuchAlgorithmException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
cript.reset();
cript.update(token.getBytes());
String password = new String(Hex.encodeHex(cript.digest()));
System.out.println(password);

Hash produced:88e7c8fc13ac75e8efc8d0c00182caa6dc087093

Upvotes: 3

Views: 1028

Answers (1)

gustafc
gustafc

Reputation: 28865

My guess is that token.getBytes() doesn't use the same encoding as Encoding.Unicode.GetBytes(token), since you're unlikely to have UTF-16 Little Endian as your default charset. What happens if you change it to token.getBytes(StandardCharsets.UTF_16LE)?

Upvotes: 3

Related Questions