1
1
/*
2
2
* The MIT License
3
3
*
4
- * Copyright 2014 Karol Bucek.
4
+ * Copyright 2014-2015 Karol Bucek.
5
5
*
6
6
* Permission is hereby granted, free of charge, to any person obtaining a copy
7
7
* of this software and associated documentation files (the "Software"), to deal
25
25
26
26
import java .security .InvalidKeyException ;
27
27
import java .security .NoSuchAlgorithmException ;
28
- import java .security .spec .InvalidKeySpecException ;
29
28
import javax .crypto .Mac ;
30
- import javax .crypto .SecretKey ;
31
- import javax .crypto .spec .PBEKeySpec ;
32
- import javax .crypto .spec .SecretKeySpec ;
29
+
30
+ import org .bouncycastle .crypto .CipherParameters ;
31
+ import org .bouncycastle .crypto .PBEParametersGenerator ;
32
+ import org .bouncycastle .crypto .generators .PKCS5S2ParametersGenerator ;
33
+ import org .bouncycastle .crypto .params .KeyParameter ;
33
34
34
35
import org .jruby .Ruby ;
35
36
import org .jruby .RubyModule ;
@@ -54,12 +55,13 @@ public static void createPKCS5(final Ruby runtime, final RubyModule ossl) {
54
55
// def pbkdf2_hmac_sha1(pass, salt, iter, keylen)
55
56
@ JRubyMethod (meta = true , required = 4 )
56
57
public static IRubyObject pbkdf2_hmac_sha1 (final IRubyObject self , final IRubyObject [] args ) {
58
+ //final byte[] pass = args[0].asString().getBytes();
57
59
final char [] pass = args [0 ].asString ().toString ().toCharArray ();
58
60
final byte [] salt = args [1 ].asString ().getBytes ();
59
61
final int iter = (int ) args [2 ].convertToInteger ().getLongValue ();
60
- final int keylen = (int ) args [3 ].convertToInteger ().getLongValue (); // e.g. 64
62
+ final int keySize = (int ) args [3 ].convertToInteger ().getLongValue (); // e.g. 64
61
63
62
- return generatePBEKey (self .getRuntime (), pass , salt , iter , keylen , "PBKDF2WithHmacSHA1" );
64
+ return generatePBEKey (self .getRuntime (), pass , salt , iter , keySize );
63
65
}
64
66
65
67
// def pbkdf2_hmac_sha1(pass, salt, iter, keylen, digest)
@@ -85,7 +87,7 @@ public static IRubyObject pbkdf2_hmac(final IRubyObject self, final IRubyObject[
85
87
final Ruby runtime = self .getRuntime ();
86
88
try {
87
89
final Mac mac = SecurityHelper .getMac ( macAlg );
88
- mac .init ( new SecretKeySpec ( pass , macAlg ) );
90
+ mac .init ( new SimpleSecretKey ( macAlg , pass ) );
89
91
final byte [] key = deriveKey (mac , salt , iter , keylen );
90
92
return StringHelper .newString (runtime , key );
91
93
}
@@ -106,20 +108,11 @@ private static String mapDigestName(final String name) {
106
108
}
107
109
108
110
private static RubyString generatePBEKey (final Ruby runtime ,
109
- final char [] pass , final byte [] salt , final int iter , final int keylen ,
110
- final String alg ) {
111
-
112
- final PBEKeySpec keySpec = new PBEKeySpec (pass , salt , iter , keylen * 8 );
113
- try {
114
- SecretKey key = SecurityHelper .getSecretKeyFactory (alg ).generateSecret (keySpec );
115
- return StringHelper .newString (runtime , key .getEncoded ());
116
- }
117
- catch (NoSuchAlgorithmException ex ) {
118
- throw Utils .newRuntimeError (runtime , ex ); // should no happen
119
- }
120
- catch (InvalidKeySpecException ex ) {
121
- throw Utils .newRuntimeError (runtime , ex ); // TODO
122
- }
111
+ final char [] pass , final byte [] salt , final int iter , final int keySize ) {
112
+ PBEParametersGenerator generator = new PKCS5S2ParametersGenerator ();
113
+ generator .init (PBEParametersGenerator .PKCS5PasswordToBytes (pass ), salt , iter );
114
+ CipherParameters params = generator .generateDerivedParameters (keySize * 8 );
115
+ return StringHelper .newString (runtime , ((KeyParameter ) params ).getKey ());
123
116
}
124
117
125
118
// http://stackoverflow.com/questions/9147463/java-pbkdf2-with-hmacsha256-as-the-prf
0 commit comments