Class: SecretSharing::Charset

Inherits:
Object
  • Object
show all
Defined in:
lib/secret_sharing/charset.rb

Overview

Charset can represent any charset which does not have the null byte

Defined Under Namespace

Classes: NotInCharset, NotPositiveInteger

Constant Summary collapse

ASCIICharset =

Charset that can represent any string that only consists of ASCII characters.

new((1..127).to_a.map(&:chr))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(charset) ⇒ Charset

The “null-byte” character is prepended to be the first character in the charset to avoid loosing the first character of the charset when it is also the first character in a string to convert.

Example

SecretSharing::Charset::DynamicCharset.new ['a', 'b', 'c'] # =>
#<SecretSharing::Charset::DynamicCharset @charset=[...]>

Parameters:

  • charset (Array)

    array of characters to use for the charset.


22
23
24
25
# File 'lib/secret_sharing/charset.rb', line 22

def initialize(charset)
  @charset = ["\u0000"] + charset
  @charset.freeze
end

Instance Attribute Details

#charsetArray (readonly)

Returns internal representation of the charset

Returns:

  • (Array)

    internal representation of the charset


10
11
12
# File 'lib/secret_sharing/charset.rb', line 10

def charset
  @charset
end

Instance Method Details

#char_to_codepoint(c) ⇒ Integer

Convert a single character into its integer representation according to the charset.

Example

charset = SecretSharing::Charset.by_charset_string 'abc'
charset.char_to_codepoint 'a'
# => 1

Parameters:

  • c (String)

    Character to retrieve its codepoint in the charset

Returns:

  • (Integer)

    Codepoint within the charset


95
96
97
98
99
100
101
# File 'lib/secret_sharing/charset.rb', line 95

def char_to_codepoint(c)
  codepoint = charset.index c
  if codepoint.nil?
    fail NotInCharset, "Char \"#{c}\" not part of the supported charset"
  end
  codepoint
end

#codepoint_to_char(codepoint) ⇒ String

Convert an integer into its string representation according to the charset. (only one character)

Example

charset = SecretSharing::Charset.by_charset_string 'abc'
charset.codepoint_to_char 1
# => "a"

Parameters:

  • codepoint (Integer)

    Codepoint to retrieve the character for

Returns:

  • (String)

    Retrieved character


77
78
79
80
81
82
# File 'lib/secret_sharing/charset.rb', line 77

def codepoint_to_char(codepoint)
  if charset.at(codepoint).nil?
    fail NotInCharset, "Codepoint #{codepoint} does not exist in charset"
  end
  charset.at(codepoint)
end

#i_to_s(input) ⇒ String

Calculate a string from an integer.

Example

charset = SecretSharing::Charset.by_charset_string 'abc'
charset.i_to_s 6
# => "ab"

Parameters:

  • input (Integer)

    integer to convert to string

Returns:

  • (String)

    converted string


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/secret_sharing/charset.rb', line 37

def i_to_s(input)
  if !input.is_a?(Integer) || input < 0
    fail NotPositiveInteger, 'input must be a non-negative integer'
  end

  output = ''
  while input > 0
    input, codepoint = input.divmod(charset.length)
    output.prepend(codepoint_to_char(codepoint))
  end
  output
end

#s_to_i(string) ⇒ String

Calculate an integer from a string.

Example

charset = SecretSharing::Charset.by_charset_string 'abc'
charset.s_to_i "ab"
# => 6

Parameters:

  • string (Integer)

    integer to convert to string

Returns:

  • (String)

    converted string


60
61
62
63
64
# File 'lib/secret_sharing/charset.rb', line 60

def s_to_i(string)
  string.chars.reduce(0) do |output, char|
    output * charset.length + char_to_codepoint(char)
  end
end

#subset?(string) ⇒ TrueClass|FalseClass

Check if the provided string can be represented by the charset.

Example

charset = SecretSharing::Charset.by_charset_string 'abc'
charset.subset? 'd'
# => false
charset.subset? 'a'
# => true

Parameters:

  • string (String)

    Character to retrieve the for codepoint

Returns:

  • (TrueClass|FalseClass)

115
116
117
# File 'lib/secret_sharing/charset.rb', line 115

def subset?(string)
  (string.chars - charset).empty?
end