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.
Defined Under Namespace
Classes: ChecksumError, DecodeError, EncodeError, Error, InvalidCharacterError, LengthTooSmallError, UnsupportedDecodingTypeError, UnsupportedEncodingTypeError
Constant Summary collapse
- ENCODED_BITS =
The number of bits encoded per symbol.
0x05
- CHECK_SYMBOL_MIN_VALUE =
The minimum value of a check symbol.
0x20
- CHECKSUM_PRIME =
The prime number used to implement error detection.
0x25
- DASH =
The ordinal value of an ASCII dash character.
"-".ord.freeze
- DECODE_ORDINALS =
Symbol values in order by encoded ASCII ordinal values.
[ 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.
[ "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.
"1.1.0"
Public Methods collapse
-
.decode(value, into: :integer, check: false, length: nil) ⇒ Integer, String
Decode a Base32 value.
-
.encode(value, length: nil, check: false) ⇒ String
Encode a value as Base32.
Class Method Details
.decode(value, into: :integer, check: false, length: nil) ⇒ Integer, String
Decode a Base32 value.
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.
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 |