module ECDSA::Format::IntegerOctetString

This module provides methods for converting integers to big endian octet strings. The conversions are defined in these sections of [SEC1](www.secg.org/collateral/sec1_final.pdf):

We use Ruby integers to represent bit strings, so this module can also be thought of as implementing these sections of SEC1:

Public Class Methods

decode(string) click to toggle source

@param string (String) @return (Integer)

# File lib/ecdsa/format/integer_octet_string.rb, line 30
def self.decode(string)
  string.bytes.reduce { |n, b| (n << 8) + b }
end
encode(integer, length) click to toggle source

@param integer (Integer) The integer to encode. @param length (Integer) The number of bytes desired in the output string. @return (String)

# File lib/ecdsa/format/integer_octet_string.rb, line 19
def self.encode(integer, length)
  raise ArgumentError, 'Integer to encode is negative.' if integer < 0
  raise ArgumentError, 'Integer to encode is too large.' if integer >= (1 << (8 * length))

  (length - 1).downto(0).map do |i|
    (integer >> (8 * i)) & 0xFF
  end.pack('C*')
end