Skip to content

Commit 91dd7f5

Browse files
committed
[fix] coerce HMAC#update input as String
1 parent c0eeb74 commit 91dd7f5

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public IRubyObject initialize_copy(final IRubyObject obj) {
168168

169169
@JRubyMethod(name = { "update", "<<" })
170170
public IRubyObject update(final IRubyObject obj) {
171-
data.append(obj.asString().getByteList());
171+
data.append(obj.convertToString().getByteList());
172172
return this;
173173
}
174174

@@ -188,10 +188,6 @@ public IRubyObject hexdigest() {
188188
return getRuntime().newString( toHEX(getSignatureBytes()) );
189189
}
190190

191-
String getAlgorithm() {
192-
return mac.getAlgorithm();
193-
}
194-
195191
private byte[] getSignatureBytes() {
196192
mac.reset();
197193
mac.update(data.getUnsafeBytes(), data.getBegin(), data.getRealSize());

src/test/ruby/test_hmac.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,20 @@ def test_hexdigest_with_empty_key
4141
assert_equal "4acb10ca3965a14a080297db0921950c", result
4242
end
4343

44+
def test_update_string_coercion
45+
h1 = OpenSSL::HMAC.new("KEY", "SHA256")
46+
h2 = OpenSSL::HMAC.new("KEY", "SHA256")
47+
48+
str_like = Object.new
49+
def str_like.to_str
50+
"DATA"
51+
end
52+
53+
h1.update(str_like)
54+
h2.update("DATA")
55+
assert_equal h2.digest, h1.digest
56+
57+
assert_raise(TypeError) { h1.update(1) }
58+
end
59+
4460
end

0 commit comments

Comments
 (0)