3434
3535import org .jruby .Ruby ;
3636import org .jruby .RubyModule ;
37+ import org .jruby .RubyNumeric ;
3738import org .jruby .RubyString ;
3839import org .jruby .anno .JRubyMethod ;
3940import org .jruby .anno .JRubyModule ;
4041import org .jruby .runtime .builtin .IRubyObject ;
4142
43+ import static org .jruby .ext .openssl .KDF .newKDFError ;
44+
4245/**
4346 * OpenSSL::PKCS5
4447 *
@@ -53,7 +56,7 @@ public static void createPKCS5(final Ruby runtime, final RubyModule ossl) {
5356 }
5457
5558 // def pbkdf2_hmac_sha1(pass, salt, iter, keylen)
56- @ JRubyMethod (meta = true , required = 4 )
59+ @ JRubyMethod (module = true , required = 4 )
5760 public static IRubyObject pbkdf2_hmac_sha1 (final IRubyObject self , final IRubyObject [] args ) {
5861 //final byte[] pass = args[0].asString().getBytes();
5962 final char [] pass = args [0 ].asString ().toString ().toCharArray ();
@@ -65,12 +68,26 @@ public static IRubyObject pbkdf2_hmac_sha1(final IRubyObject self, final IRubyOb
6568 }
6669
6770 // def pbkdf2_hmac_sha1(pass, salt, iter, keylen, digest)
68- @ JRubyMethod (meta = true , required = 5 )
71+ @ JRubyMethod (module = true , required = 5 )
6972 public static IRubyObject pbkdf2_hmac (final IRubyObject self , final IRubyObject [] args ) {
70- final byte [] pass = args [0 ].asString ().getBytes ();
71- final byte [] salt = args [1 ].asString ().getBytes ();
72- final int iter = (int ) args [2 ].convertToInteger ().getLongValue ();
73- final int keylen = (int ) args [3 ].convertToInteger ().getLongValue ();
73+ final Ruby runtime = self .getRuntime ();
74+ try {
75+ return pbkdf2Hmac (runtime , args );
76+ }
77+ catch (NoSuchAlgorithmException ex ) {
78+ throw Utils .newRuntimeError (runtime , ex ); // should no happen
79+ }
80+ catch (InvalidKeyException ex ) {
81+ throw newKDFError (runtime , ex .getMessage ()); // in MRI PKCS5 delegates to KDF impl
82+ }
83+ }
84+
85+ static RubyString pbkdf2Hmac (final Ruby runtime , final IRubyObject [] args )
86+ throws NoSuchAlgorithmException , InvalidKeyException {
87+ final byte [] pass = args [0 ].convertToString ().getBytes ();
88+ final byte [] salt = args [1 ].convertToString ().getBytes ();
89+ final int iter = RubyNumeric .num2int (args [2 ]);
90+ final int keylen = RubyNumeric .num2int (args [3 ]);
7491
7592 final String digestAlg ;
7693 final IRubyObject digest = args [4 ];
@@ -83,20 +100,15 @@ public static IRubyObject pbkdf2_hmac(final IRubyObject self, final IRubyObject[
83100
84101 // NOTE: on our own since e.g. "PBKDF2WithHmacMD5" not supported by Java
85102
103+ // key = SecurityHelper.getSecretKeyFactory("PBKDF2WithHmac" + hash).generateSecret(spec);
104+ // return StringHelper.newString(runtime, key.getEncoded());
105+
86106 final String macAlg = "Hmac" + digestAlg ;
87- final Ruby runtime = self .getRuntime ();
88- try {
89- final Mac mac = SecurityHelper .getMac ( macAlg );
90- mac .init ( new SimpleSecretKey (macAlg , pass ) );
91- final byte [] key = deriveKey (mac , salt , iter , keylen );
92- return StringHelper .newString (runtime , key );
93- }
94- catch (NoSuchAlgorithmException ex ) {
95- throw Utils .newRuntimeError (runtime , ex ); // should no happen
96- }
97- catch (InvalidKeyException ex ) {
98- throw Utils .newRuntimeError (runtime , ex ); // TODO
99- }
107+
108+ final Mac mac = SecurityHelper .getMac ( macAlg );
109+ mac .init ( new SimpleSecretKey (macAlg , pass ) );
110+ final byte [] key = deriveKey (mac , salt , iter , keylen );
111+ return StringHelper .newString (runtime , key );
100112 }
101113
102114 private static String mapDigestName (final String name ) {
@@ -107,18 +119,15 @@ private static String mapDigestName(final String name) {
107119 return mapped ;
108120 }
109121
110- private static RubyString generatePBEKey (final Ruby runtime ,
122+ static RubyString generatePBEKey (final Ruby runtime ,
111123 final char [] pass , final byte [] salt , final int iter , final int keySize ) {
112124 PBEParametersGenerator generator = new PKCS5S2ParametersGenerator ();
113125 generator .init (PBEParametersGenerator .PKCS5PasswordToBytes (pass ), salt , iter );
114126 CipherParameters params = generator .generateDerivedParameters (keySize * 8 );
115127 return StringHelper .newString (runtime , ((KeyParameter ) params ).getKey ());
116128 }
117129
118- // http://stackoverflow.com/questions/9147463/java-pbkdf2-with-hmacsha256-as-the-prf
119-
120- public static byte [] deriveKey ( final Mac prf , byte [] salt , int iterationCount , int dkLen )
121- throws NoSuchAlgorithmException , InvalidKeyException {
130+ public static byte [] deriveKey ( final Mac prf , byte [] salt , int iterationCount , int dkLen ) {
122131
123132 // Note: hLen, dkLen, l, r, T, F, etc. are horrible names for
124133 // variables and functions in this day and age, but they
0 commit comments