简体   繁体   中英

How to convert String to SHA-256 hash in J2ME?

The application I'm working on requires a String to be converted to a SHA-256 hash, but since it's J2ME I don't have access to MessageDigest full functionality.

I tried BouncyCastle library but when I compare the resulting hash with the result from an online page, the values are different.

Another way for me to know the hash wasn't created right is to send it to my server in a HTTP request, if I can login then the hash was created correctly, else it was malformed.

I've been told that I should implement my own SHA-256 enconding but so far I haven't found examples or the algorithm itself, only the pseudocode from the Wikipedia page and worse I don't have experience in cryptography at all.

Do I have to create my own implementation? or is there still chance for me to avoid this and use a different library?

Here is the current piece of code I tried with BouncyCastle:

            String[] stringArray = (String[]) dotREZApi.getCurrentRequest().getResponseHeaders().get("X-Session-Id");
            String tmpSessionId = stringArray[0];

            byte[] input = null;
            try {
                input = new String("X-Session-Sig" + tmpSessionId + "test").getBytes("UTF-8"); // String that will be converted
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                System.out.println("Unsupported encoding exception");
                e.printStackTrace();
            }

            // trying to create a key, i don't know if this is right I just did this because I needed a key             
            byte[] key = new byte[4];
            int tmpInt = new Random().nextInt();
            key[0] = (byte)(tmpInt >> 24);
            key[1] = (byte)(tmpInt >> 16);
            key[2] = (byte)(tmpInt >> 8);
            key[3] = (byte)(tmpInt);


            Digest digest = new SHA256Digest();
            HMac hmac = new HMac(digest);

            hmac.init(new KeyParameter(key)); // maybe this is the problem? the documentaiton never states where do I get this key or how to generate it
            hmac.update(input, 0, input.length);
            byte[] output = new byte[digest.getDigestSize()];
            hmac.doFinal(output, 0);
            dotREZApi.setSessionSig(new String(Hex.encode(output)));

The result I get from this I compare to the one I get from this site , but I don't get match.

The line input = new String("X-Session-Sig" + tmpSessionId + "test").getBytes("UTF-8"); is where I construct the string and then convert it to byte[] to convert to SHA-256 hash, where tmpSessionId is a value I get from a HTTP request. The resulting hash will be sent in future requests.

In the end I didn't use BouncyCastle.

I ended up using MessageDigest from J2ME, and while it doesn't have the same methods available, I could get it to work:

    byte[] input = null;
    try {
        input = new String("X-Session-Id" + tmpSessionId + "test").getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        System.out.println("Unsupported encoding exception");
        e.printStackTrace();
    }

    MessageDigest messageDigest;
    byte[] output = new byte[32]; // since SHA256 hash is 256 bits = 32 bytes
    try {
        messageDigest = MessageDigest.getInstance("SHA-256");
        messageDigest.update(input, 0, input.length);
        messageDigest.digest(output, 0, 32); // same
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DigestException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

I hope this alternative to BouncyCastle works for others too.

If someone could unmark this question as a duplicated since my problem was with BouncyCastle and was looking for an alternative to it since BouncyCastle is what most pages recommend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM