Module: Secp256k1::Util

Defined in:
lib/rbsecp256k1/util.rb

Overview

Contains utility methods that complement the functionality of the library.

Constant Summary collapse

HEX_REGEXP =

Regexp to validate a hexadecimal string

/^[A-Fa-f\d]+$/.freeze

Class Method Summary collapse

Class Method Details

.bin_to_hex(binary_string) ⇒ String

Converts a binary string to a hex string.

Parameters:

  • binary_string (String)

    binary string to be converted.

Returns:

  • (String)

    hex string equivalent of the given binary string.



13
14
15
# File 'lib/rbsecp256k1/util.rb', line 13

def self.bin_to_hex(binary_string)
  binary_string.unpack('H*').first
end

.hex_to_bin(hex_string) ⇒ String

Converts a hex string to a binary string.

Parameters:

  • hex_string (String)

    string with hexadeimcal value.

Returns:

  • (String)

    binary string equivalent of the given hex string.

Raises:

  • (ArgumentError)

    if hex string is an invalid hexadecimal string.



22
23
24
25
26
# File 'lib/rbsecp256k1/util.rb', line 22

def self.hex_to_bin(hex_string)
  raise ArgumentError, "Invalid hexadecimal string" unless hex_string =~ HEX_REGEXP

  [hex_string].pack('H*')
end