Skip to content

Commit 051964d

Browse files
committed
use our SimpleKey impl so that there's less[] copy
... also allows for an empty key to work like MRI (fixes jruby/jruby#2854)
1 parent 240918c commit 051964d

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/main/java/org/jruby/ext/openssl/HMAC.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static IRubyObject digest(IRubyObject self, IRubyObject digest, IRubyObje
8787
final ByteList bytes = data.asString().getByteList();
8888
try {
8989
Mac mac = getMacInstance(algName);
90-
mac.init( new SecretKeySpec(keyBytes, mac.getAlgorithm()) );
90+
mac.init( new SimpleSecretKey(mac.getAlgorithm(), keyBytes) );
9191
mac.update(bytes.getUnsafeBytes(), bytes.getBegin(), bytes.getRealSize());
9292
return runtime.newString( new ByteList(mac.doFinal(), false) );
9393
}
@@ -108,7 +108,7 @@ public static IRubyObject hexdigest(IRubyObject self, IRubyObject digest, IRubyO
108108
final ByteList bytes = data.asString().getByteList();
109109
try {
110110
final Mac mac = getMacInstance(algName);
111-
mac.init( new SecretKeySpec(keyBytes, mac.getAlgorithm()) );
111+
mac.init( new SimpleSecretKey(mac.getAlgorithm(), keyBytes) );
112112
mac.update(bytes.getUnsafeBytes(), bytes.getBegin(), bytes.getRealSize());
113113
return runtime.newString( toHEX( mac.doFinal() ) );
114114
}
@@ -135,7 +135,7 @@ public IRubyObject initialize(IRubyObject key, IRubyObject digest) {
135135
try {
136136
this.mac = getMacInstance(algName);
137137
this.key = key.asString().getBytes();
138-
mac.init( new SecretKeySpec(this.key, mac.getAlgorithm()) );
138+
mac.init( SimpleSecretKey.copy(mac.getAlgorithm(), this.key) );
139139
}
140140
catch (NoSuchAlgorithmException e) {
141141
throw getRuntime().newNotImplementedError("Unsupported MAC algorithm (HMAC[-]" + algName + ")");
@@ -159,7 +159,7 @@ public IRubyObject initialize_copy(final IRubyObject obj) {
159159
try {
160160
this.mac = SecurityHelper.getMac(algName);
161161
this.key = that.key;
162-
mac.init( new SecretKeySpec(key, algName) );
162+
mac.init( SimpleSecretKey.copy(algName, key) );
163163
}
164164
catch (NoSuchAlgorithmException e) {
165165
throw getRuntime().newNotImplementedError("Unsupported MAC algorithm (" + algName + ")");

src/test/ruby/test_hmac.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,10 @@ def test_correct_digest
3535
@h2.update("\xFF") # invalid utf-8 char
3636
assert_equal('0770623462e782b51bb0689a8ba4f3f1', @h2.hexdigest) # calcualted on MRI
3737
end
38+
39+
def test_hexdigest_with_empty_key
40+
result = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('md5'), "", "foo")
41+
assert_equal "4acb10ca3965a14a080297db0921950c", result
42+
end
43+
3844
end

0 commit comments

Comments
 (0)