2

I want to perform SHA256 hashing in a Blackberry application. Searching, I found the Bouncy Castle project has a crypto library for this, but I can't find any samples to show how to use SHA256 hashing.

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
user598312
  • 31
  • 1
  • 2
  • Duplicate of http://stackoverflow.com/questions/3103652/hash-string-via-sha-256-in-java – Michael Donohue Feb 17 '11 at 19:44
  • Not a duplicate because it specifically asks for a Bouncycastle example. – President James K. Polk Feb 20 '11 at 22:14
  • Why aren't the javadocs for [SHA256Digest](http://www.bouncycastle.org/docs/docs1.6/org/bouncycastle/crypto/digests/SHA256Digest.html) adequate? – President James K. Polk Feb 20 '11 at 22:15
  • 1
    Yea,i got the way to use Bouncy Castle crypto api for SHA256 encoding in J2ME.Use it as below. private static byte[] getSHA256(String key){ SHA256Digest digester=new SHA256Digest(); byte[] retValue=new byte[digester.getDigestSize()]; digester.update(key.getBytes(), 0, key.length()); digester.doFinal(retValue, 0); return retValue; } ~Ragesh Kumar AK – user598312 Feb 21 '11 at 08:12

2 Answers2

2

Just reposting user598312's answer as a response instead of a comment, so people know the solution.

private static byte[] getSHA512(String key) {
    SHA512Digest digester = new SHA512Digest();
    byte[] retValue = new byte[digester.getDigestSize()];
    digester.update(key.getBytes(), 0, key.length());
    digester.doFinal(retValue, 0);
    return retValue;
}
Ajibola
  • 1,218
  • 16
  • 28
  • 1
    `key.length` should be `key.getBytes().length` as java is UTF-16 so the string length may be different from the byte length depending on the input. Some platforms are UTF-8 (i.e. Android) but using the byte[] length will make this code more robust :) – Dori Mar 20 '14 at 18:03
0

BlackBerry has built-in implementations of nearly everything in the BouncyCastle API. For SHA256, there is SHA256Digest.

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44