Module: Crockford32

Defined in:
lib/crockford32.rb,
lib/crockford32/errors.rb,
lib/crockford32/version.rb

Overview

A fast little-endian implementation of Douglas Crockford’s Base32 specification.

Since:

  • 1.0.0

Defined Under Namespace

Classes: ChecksumError, DecodeError, EncodeError, Error, InvalidCharacterError, LengthTooSmallError, UnsupportedDecodingTypeError, UnsupportedEncodingTypeError

Constant Summary collapse

ENCODED_BITS =

The number of bits encoded per symbol.

Since:

  • 1.0.0

0x05
CHECK_SYMBOL_MIN_VALUE =

The minimum value of a check symbol.

Since:

  • 1.0.0

0x20
CHECKSUM_PRIME =

The prime number used to implement error detection.

Since:

  • 1.0.0

0x25
DASH =

The ordinal value of an ASCII dash character.

Since:

  • 1.0.0

"-".ord.freeze
DECODE_ORDINALS =

Symbol values in order by encoded ASCII ordinal values.

Since:

  • 1.0.0

[
  nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
  nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
  nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
   34, nil, nil, nil, nil, nil,  32, nil, nil, nil, nil, nil,
    0,   1,   2,   3,   4,   5,   6,   7,   8,   9, nil, nil,
  nil,  35, nil, nil, nil,  10,  11,  12,  13,  14,  15,  16,
   17,   1,  18,  19,   1,  20,  21,   0,  22,  23,  24,  25,
   26,  36,  27,  28,  29,  30,  31, nil, nil, nil, nil, nil,
  nil,  10,  11,  12,  13,  14,  15,  16,  17,   1,  18,  19,
    1,  20,  21,   0,  22,  23,  24,  25,  26,  36,  27,  28,
   29,  30,  31, nil, nil, nil,  33
].freeze
ENCODE_SYMBOLS =

Encoding symbols ordered by bit value.

Since:

  • 1.0.0

[
  "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F",
  "G", "H", "J", "K", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z",
  "*", "~", "$", "=", "U"
].freeze
VERSION =

The library version.

Since:

  • 1.0.0

"1.1.0"

Public Methods collapse

Class Method Details

.decode(value, into: :integer, check: false, length: nil) ⇒ Integer, String

Decode a Base32 value.

Parameters:

  • value (String)

    the Base32 value to decode.

  • into (Symbol) (defaults to: :integer)

    the destination type to decode into. Can be :integer or :string.

  • check (Boolean) (defaults to: false)

    whether to validate the check symbol.

  • length (Integer, nil) (defaults to: nil)

    the length of the resulting string when right padded.

Returns:

  • (Integer, String)

    the decoded value.

Raises:

Since:

  • 1.0.0



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/crockford32.rb', line 65

def self.decode(value, into: :integer, check: false, length: nil)
  checksum = check ? value[-1] : nil
  value = check ? value[0...-1] : value

  result = le_decode_number value

  if check
    actual = result % CHECKSUM_PRIME
    required = DECODE_ORDINALS[checksum.ord]
    raise ChecksumError.new(value, actual, required) if actual != required
  end

  convert result, into, length
end

.encode(value, length: nil, check: false) ⇒ String

Encode a value as Base32.

Parameters:

  • value (Integer, String)

    the value to encode.

  • length (Integer) (defaults to: nil)

    the exact length of the Base32 string. Will be padded with “0” to meet length.

  • check (Boolean) (defaults to: false)

    whether to include a check symbol. This symbol is included in the length.

Returns:

  • (String)

    the encoded value.

Raises:

Since:

  • 1.0.0



92
93
94
# File 'lib/crockford32.rb', line 92

def self.encode(value, length: nil, check: false)
  le_encode_number(raw_value_to_number(value), length, check)
end