Java String to SHA1
UPDATE
You can use Apache Commons Codec (version 1.7+) to do this job for you.
DigestUtils.sha1Hex(stringToConvertToSHexRepresentation)
Thanks to @Jon Onstott for this suggestion.
Old Answer
Convert your Byte Array to Hex String. Real's How To tells you how.
return byteArrayToHexString(md.digest(convertme))
and (copied from Real's How To)
public static String byteArrayToHexString(byte[] b) {
String result = "";
for (int i=0; i < b.length; i++) {
result +=
Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return result;
}
BTW, you may get more compact representation using Base64. Apache Commons Codec API 1.4, has this nice utility to take away all the pain. refer here
Java Make String to SHA1
You have to print the bytes in your array, and you'd likely want to display the hash as hex.
for(byte b : SHA1 ) {
System.out.printf("%02x",b);
}
System.out.println();
Java calculate hex representation of a SHA-1 digest of a String
This is happening because cript.digest() returns a byte array, which you're trying to print out as a character String. You want to convert it to a printable Hex String.
Easy solution: Use Apache's commons-codec library:
String password = new String(Hex.encodeHex(cript.digest()),
CharSet.forName("UTF-8"));
SHA1 hashing not working as expected in Java
When you use String sample = new String(byte[] bytes)
it will create a string with platform's default charset, your digest bytes may not have alphanumeric representation in that charset.
Try to use Base64 or HexString to display digest message.
For example in JAVA8:
You can encode your digest bytes to string with:
String hashstr = Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA1").digest(str.getBytes("UTF-8")));
You can decode your Base64 with:
byte [] digest = Base64.getDecoder().decode(hashstr);
How to Hash String using SHA-1 with key?
Try something like that:
private String sha1(String s, String keyString) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(key);
byte[] bytes = mac.doFinal(s.getBytes("UTF-8"));
return new String( Base64.encodeBase64(bytes));
}
SecretKeySpec docs.
Convert ComputeHash using SHA1 algorithm in C# to Java
I've changed code and get same output for C# and Java.
Here is my Java Code :
public static String ComputeHash(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException{
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.reset();
md.update(password.getBytes("UTF-8"));
return toHexString(md.digest());
}
private static String toHexString(byte[] data){
Formatter formatter = new Formatter();
for(byte b : data){
formatter.format("%02x", b);
}
String result = formatter.toString();
formatter.close();
return result;
}
With same string input : "abc123", I got same result : 6367C48DD193D56EA7B0BAAD25B19455E529F5EE
Thanks M. Schena , I got my solution in your comment. Thank so much !
SHA-1 hashing on Java and C#
Try using this as your hashing in C#:
static string Hash(string input)
{
using (SHA1Managed sha1 = new SHA1Managed())
{
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
var sb = new StringBuilder(hash.Length * 2);
foreach (byte b in hash)
{
// can be "x2" if you want lowercase
sb.Append(b.ToString("x2"));
}
return sb.ToString();
}
}
Hash("test"); //a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
And then use this as your Java hashing:
private static String convertToHex(byte[] data) {
StringBuilder buf = new StringBuilder();
for (byte b : data) {
int halfbyte = (b >>> 4) & 0x0F;
int two_halfs = 0;
do {
buf.append((0 <= halfbyte) && (halfbyte <= 9) ? (char) ('0' + halfbyte) : (char) ('a' + (halfbyte - 10)));
halfbyte = b & 0x0F;
} while (two_halfs++ < 1);
}
return buf.toString();
}
public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] textBytes = text.getBytes("iso-8859-1");
md.update(textBytes, 0, textBytes.length);
byte[] sha1hash = md.digest();
return convertToHex(sha1hash);
}
SHA1("test"); //a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
Note you need the following imports:
import java.io.UnsupportedEncodingException; import
java.security.MessageDigest; import
java.security.NoSuchAlgorithmException;
Throws declarations are option, adjust to best fit your code!
How to SHA1 hash a string in Android?
You don't need andorid for this. You can just do it in simple java.
Have you tried a simple java example and see if this returns the right sha1.
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class AeSimpleSHA1 {
private static String convertToHex(byte[] data) {
StringBuilder buf = new StringBuilder();
for (byte b : data) {
int halfbyte = (b >>> 4) & 0x0F;
int two_halfs = 0;
do {
buf.append((0 <= halfbyte) && (halfbyte <= 9) ? (char) ('0' + halfbyte) : (char) ('a' + (halfbyte - 10)));
halfbyte = b & 0x0F;
} while (two_halfs++ < 1);
}
return buf.toString();
}
public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] textBytes = text.getBytes("iso-8859-1");
md.update(textBytes, 0, textBytes.length);
byte[] sha1hash = md.digest();
return convertToHex(sha1hash);
}
}
Also share what your expected sha1 should be. Maybe ObjectC is doing it wrong.
Related Topics
How to Create Executable Java Program
How to Have 2 Jvms Talk to One Another
How to Set Up Jax-Rs Application Using Annotations Only (No Web.Xml)
Compile Code Fully in Memory with Javax.Tools.Javacompiler
Double Checked Locking in Singleton
In Java, What Is a Shallow Copy
Given Final Block Not Properly Padded
Booleans, Conditional Operators and Autoboxing
Send an Array with an Http Get
When Will a String Be Garbage Collected in Java
Sorting Java Objects Using Multiple Keys
Create Java Console Inside a Gui Panel
How to Convert Java String to Date Object
How to Check If Two Words Are Anagrams
Noclassdeffounderror on Maven Dependency
Java: Detect Duplicates in Arraylist