SHA-256 hashing function in Java ME/J2ME

大兔子大兔子 提交于 2019-12-01 12:45:45

问题


I've posted this question on the Nokia Developer forums so please bear with me.

I'm writing an app which needs to find the SHA-256 hash of a URL keyed with a unique value – i.e. hmac('sha256', '27/3', '9EWVFmHpHN6n2YKW9QtvUqX3xbsFQUBovlrFddqnF7fpcSDA2q'). What would be the best way to do this in Java ME/J2ME?

I've found many examples using the Mac class but this isn't supported in Java ME/J2ME.

Thanks in advance.


回答1:


I managed to get things working, the solution is as follows:

Digest  digest = new SHA256Digest();
HMac hmac = new HMac(digest);
hmac.init(new KeyParameter(appKeyHere));
hmac.update(requestURI, 0, lenOfReqURI);
byte[]  resBuf = new byte[digest.getDigestSize()];
hmac.doFinal(resBuf, 0);
String  resStr = new String(Hex.encode(resBuf)); // Contains final usable value



回答2:


BouncyCastle's latest J2ME compatible release (the lightweight API) contains among other things an SHA256 implementation - org.bouncycastle.crypto.digests.SHA256Digest - that should work for you.



来源:https://stackoverflow.com/questions/14568728/sha-256-hashing-function-in-java-me-j2me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!