Skip to content

Commit 725edfc

Browse files
improve encoding of oid in order to avoid costly intermediate array creation
1 parent 4b6ba50 commit 725edfc

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

lib/openssl/asn1.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,27 @@ class BMPString < Primitive
333333

334334
class ObjectId < Primitive
335335
def der_value
336-
value = oid.split(".").map(&:to_i)
336+
value = oid
337337

338-
return (40 * value[0]).chr if value.length == 1
338+
dot_index = value.index(".")
339339

340-
[value[0] * 40 + value[1], *value[2..]].pack("w*")
340+
if dot_index == value.size - 1
341+
return (value.to_i * 40).chr
342+
else
343+
codes = [value.byteslice(0..dot_index-1).to_i * 40]
344+
end
345+
346+
add_to_top = false
347+
value.byteslice(dot_index+1..-1).split(".") do |sub|
348+
if add_to_top
349+
codes << sub.to_i
350+
else
351+
codes[0] += sub.to_i
352+
add_to_top = true
353+
end
354+
end
355+
356+
codes.pack("w*")
341357
end
342358
end
343359

0 commit comments

Comments
 (0)