Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java sha1 and PHP sha1

807580Apr 25 2007 — edited Dec 22 2009
Hello to everyone.
When encoding with PHP using sha1 i do not get the same result as Java's sha1 encoding.
For example encoding letter 'e' in PHP produces:
58e6b3a414a1e0[b]90dfc6029add0f3555ccba127f
whereas in Java it produces:
58e6b3a414a1e0[b]3fdfc6029add0f3555ccba127f

Below is the code i am using for both Java and PHP
String password=pass;
		java.security.MessageDigest d =null;
		try {
			d = java.security.MessageDigest.getInstance("SHA-1");
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		d.reset();
		d.update(password.getBytes());
		String secret = new String (d.digest());
		secret.getBytes();
		StringBuffer sb = new StringBuffer(secret.getBytes().length * 2);
        for (int i = 0; i < secret.getBytes().length; i++) {
             int b = secret.getBytes() & 0xFF;
sb.append(HEX_DIGITS.charAt(b >>> 4)).append(HEX_DIGITS.charAt(b & 0xF));
}
return sb.toString();


PHP Code:
$str = 'e';
$str2=sha1($str2);
echo $str2;
Anyone had similar problems and found a solution to them? I cannot see any mistake in the code so any help would be much appreciated.
P.S: I am running Java 1.4 and PHP 5.1.6 configuration

Kind Regards
Christophoros
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 19 2010
Added on Apr 25 2007
4 comments
777 views