Results 1 to 1 of 1
I have samll doubt in SHA 256 algorith, Using sha256 alogorith i write a smalll java program that will generate 30 charecters of hash value for my given string, But ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 12-15-2008 #1Just Joined!
- Join Date
- Dec 2008
- Posts
- 1
SHA256 algorithanm giving unexpected behaviour on linux
I have samll doubt in SHA 256 algorith, Using sha256 alogorith i write a smalll java program that will generate 30 charecters of hash value for my given string, But when i ran same program on linux(64 bit ) os. I am getting 70 charecter of hash value. do you have any idea
i am giving my code here
import java.io.*;
import sun.misc.*;
import org.apache.commons.codec.binary.*;
import java.security.MessageDigest;
import java.net.URLEncoder;
import com.sun.org.apache.xerces.internal.impl.dv.util.Ba se64;
class SHA256
{
public static void main(String args[]) throws Exception
{
String msisdn = "7950977702";
//Performing SHA256
byte[] encoded = null;
String sha256EncodedString = null;
MessageDigest md = MessageDigest.getInstance("SHA-1");
try
{
encoded = md.digest(msisdn.getBytes());
System.out.println("sha value: " + encoded);
sha256EncodedString = new String(encoded);
System.out.println("\nEncoded sha 256 value: " + sha256EncodedString);
String bytesToBase64String = null;
bytesToBase64String = Base64.encode(sha256EncodedString.getBytes());
System.out.println("\nAfter encoding using Apache xerces encoding SHA256/BASE64 :"+ bytesToBase64String.trim());
bytesToBase64String = bytesToBase64String.substring(0, bytesToBase64String.indexOf("="));
System.out.println("\nAfter encoding using Apache xerces encoding/paddding SHA256/BASE64 :"+ bytesToBase64String.trim());
org.apache.commons.codec.binary.Base64 base64 = new org.apache.commons.codec.binary.Base64();
String baseEncode = base64.encode(sha256EncodedString.getBytes()).toSt ring();
System.out.println("\nAfter encoding using Apache encoding SHA256/BASE64 :"+ baseEncode.trim());
BASE64Encoder encoder = new BASE64Encoder();
String sunBaseEncode = encoder.encodeBuffer(sha256EncodedString.getBytes( ));
System.out.println("\nAfter encoding using suns encoding SHA256/BASE64 :"+ sunBaseEncode.trim());
} catch (Exception e)
{
System.out.println("Exception " + e);
}
}
}


Reply With Quote
