gnu-crypto-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNU Crypto] Having trouble with PBKDF2WithHmacSHA256


From: Aaron Zinman
Subject: [GNU Crypto] Having trouble with PBKDF2WithHmacSHA256
Date: Thu, 31 May 2012 19:25:53 -0400

Hi everyone,

First, thanks for offering a giant alternative to Sun!

I'm trying to move to the JVM from Python where I was using Passlib to
authenticate a web service using their PBKDF2 with HMAC SHA-256
implementation. I'm now trying to replicate it in Scala and keep
banging my head against the wall.

I know that GnuCrypto supports "PBKDF2WithHmacSHA256" but I cannot for
the life of me figure out how to get it to work. Please forgive my
noob-ness.

I'm getting an NullPointerException on the HMac's constructor (from
the Scala REPL....):

scala> import javax.crypto.spec.PBEKeySpec
etc....

scala> java.security.Security.addProvider(new gnu.crypto.jce.GnuCrypto())
res0: Int = 10

scala> val salt = Random.generate(128) // my own wrapper for SecureRandom
salt: Array[Byte] = Array(15, 1, -27, -51, 123, 50, 41, -81, 96, -42,
-101, 15, -59, 15, 56, -93, 26, 64, 86, 4, 119, 91, 97, 88, -12, -33,
103, -100, -84, -25, 51, -39, -122, 48, 95, 102, 5, 110, -102, -82,
-32, 50, 88, -67, -124, -110, 76, 17, -100, 43, 108, 96, 32, -122,
-55, 40, -15, 16, -1, 13, -112, -58, 5, 45, -93, 25, 82, 54, 56, -105,
37, 67, -66, 88, 16, -126, 90, -55, -54, 60, -81, 64, 86, 3, -122, 45,
-11, -15, -24, 95, 2, 50, 38, 88, 17, 95, 66, -115, 13, 83, -65, 30,
-100, -54, 23, -38, -105, 59, 126, -79, -3, -17, -50, -5, 52, 8, -7,
79, 121, 33, 15, -63, 6, 74, 20, -122, -86, -76)

scala> val f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256")
f: javax.crypto.SecretKeyFactory = address@hidden

scala> val spec = new PBEKeySpec("test".toCharArray(), salt, 10000, 256)
spec: javax.crypto.spec.PBEKeySpec = address@hidden

scala> val checksum = f.generateSecret(spec).getEncoded
java.security.spec.InvalidKeySpecException: Could not generate secret key
       at 
javax.crypto.SecretKeyFactory.generateSecret(SecretKeyFactory.java:346)
       at .<init>(<console>:20)
       at .<clinit>(<console>)
       at .<init>(<console>:11)
       at .<clinit>(<console>)
       at $print(<console>)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
       at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.lang.reflect.Method.invoke(Method.java:601)
       at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)
       at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:914)
       at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:546)
       at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:577)
       at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:543)
       at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:694)
       at 
scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:745)
       at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:651)
       at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:542)
       at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:550)
       at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822)
       at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:851)
       at xsbt.ConsoleInterface.run(ConsoleInterface.scala:57)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
       at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.lang.reflect.Method.invoke(Method.java:601)
       at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:57)
       at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:48)
       at sbt.Console.console0$1(Console.scala:23)
       at 
sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24)
       at sbt.TrapExit$.executeMain$1(TrapExit.scala:33)
       at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)
Caused by: java.lang.NullPointerException
       at gnu.crypto.mac.HMac.<init>(HMac.java:120)
       at gnu.crypto.mac.HMacFactory.getInstance(HMacFactory.java:101)
       at gnu.crypto.mac.MacFactory.getInstance(MacFactory.java:90)
       at gnu.crypto.prng.PRNGFactory.getInstance(PRNGFactory.java:104)
       at 
gnu.crypto.jce.PBKDF2SecretKeyFactory.engineGenerateSecret(PBKDF2SecretKeyFactory.java:88)
       at 
javax.crypto.SecretKeyFactory.generateSecret(SecretKeyFactory.java:335)
       ... 32 more


I welcome your advice!

Thanks,
Aaron



reply via email to

[Prev in Thread] Current Thread [Next in Thread]