Class: SecureString

Inherits:
String
  • Object
show all
Includes:
SecurizeString
Defined in:
lib/secure_string.rb

Overview

SecureString is a String subclass whose emphasis is on byte data rather than human readable strings. class gives a number of conveniences, such as easier viewing of the byte data as hex, digest methods, and encryption and decryption methods.

Instance Method Summary collapse

Methods included from SecurizeString

included

Constructor Details

#initialize(value, opts = {}) ⇒ SecureString

Creates the string from one many kinds of values:

:data

(default) The passed string value is directly used.

:hex

Initialize using a hexidecimal string.

:int

Initialize using the numeric value of the hexidecimal string.

:base64

Initialize using the given base64 encoded data.

To specify a value kind, pass the :type option.

Raises:

  • (ArgumentError)


16
17
18
19
20
# File 'lib/secure_string.rb', line 16

def initialize(value, opts={})
  raise ArgumentError, "The first argument is a symbol; setting the input data type this way is no longer supported. Call `#{self.class.name}.new('string', :type => #{value.inspect})' instead.", caller if value.kind_of?(Symbol)
  data_string = self.class.parse_data(value, opts)
  self.replace(data_string)
end

Instance Method Details

#inspectObject

Override the default inspect to return the hexidecimal representation of the data contained in this string.



35
36
37
# File 'lib/secure_string.rb', line 35

def inspect
  return "<#{to_hex}>"
end

#to_hexObject

Add a method to convert the internal binary data into a hex string.



29
30
31
# File 'lib/secure_string.rb', line 29

def to_hex
  return data_to_hex
end

#to_iObject

Override the default to_i method to return the integer value of the data contained in the string, rather than the parsed value of the characters.



24
25
26
# File 'lib/secure_string.rb', line 24

def to_i
  return data_to_i
end