Class: Virgil::Crypto::Bytes

Inherits:
Array
  • Object
show all
Defined in:
lib/virgil/crypto/bytes.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_base64(str) ⇒ Object

Initializes a new array of bytes from specified string, which encodes binary data as base-64 digits.



84
85
86
# File 'lib/virgil/crypto/bytes.rb', line 84

def self.from_base64(str)
  new(Base64.decode64(str).bytes)
end

.from_hex(str) ⇒ Object

Initializes a new array of bytes from specified string, which encodes binary data as hexadecimal digits.



96
97
98
# File 'lib/virgil/crypto/bytes.rb', line 96

def self.from_hex(str)
  new(str.scan(/../).map { |x| x.hex })
end

.from_string(str, encoding = VirgilStringEncoding::UTF8) ⇒ Object

Initializes a new array of bytes from specified string, which encodes binary data.

Parameters:

  • str (String)

    String to decode.

  • encoding (VirgilStringEncoding) (defaults to: VirgilStringEncoding::UTF8)

    The character encoding of string.

Raises:

  • (ArgumentError)

    if encoding is undefined



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/virgil/crypto/bytes.rb', line 45

def self.from_string(str, encoding = VirgilStringEncoding::UTF8)
  case encoding
  when VirgilStringEncoding::BASE64
    from_base64(str)
  when VirgilStringEncoding::HEX
    from_hex(str)
  when VirgilStringEncoding::UTF8
    from_utf8(str)
  else
    raise ArgumentError, 'Encoding is undefined'
  end
end

.from_utf8(str) ⇒ Object

Initializes a new array of bytes from specified string, which encodes binary data as utf8.



90
91
92
# File 'lib/virgil/crypto/bytes.rb', line 90

def self.from_utf8(str)
  new(str.bytes)
end

Instance Method Details

#to_base64Object

Converts all the bytes to its equivalent string representation that is encoded with base-64 digits.



102
103
104
# File 'lib/virgil/crypto/bytes.rb', line 102

def to_base64
  Base64.strict_encode64(to_s)
end

#to_hexObject

Converts the numeric value of each element of a current array of bytes to its equivalent hexadecimal string representation.



113
114
115
# File 'lib/virgil/crypto/bytes.rb', line 113

def to_hex
  to_s.unpack('H*').first
end

#to_sObject

Converts all the bytes to its equivalent string representation in utf8.



78
79
80
# File 'lib/virgil/crypto/bytes.rb', line 78

def to_s
  pack('c*')
end

#to_string(encoding = VirgilStringEncoding::UTF8) ⇒ String

Decodes the current bytes to a string according to the specified character encoding.

Parameters:

  • encoding (VirgilStringEncoding) (defaults to: VirgilStringEncoding::UTF8)

    The character encoding to encode to. equivalent string representation if raw bytes in selected encoding.

Returns:

  • (String)

Raises:

  • (ArgumentError)

    if encoding is undefined



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/virgil/crypto/bytes.rb', line 64

def to_string(encoding = VirgilStringEncoding::UTF8)
  case encoding
  when VirgilStringEncoding::BASE64
    to_base64
  when VirgilStringEncoding::HEX
    to_hex
  when VirgilStringEncoding::UTF8
    to_s
  else
    raise ArgumentError, 'Encoding is undefined'
  end
end

#to_utf8Object

Encodes all the bytes into a utf8 string.



107
108
109
# File 'lib/virgil/crypto/bytes.rb', line 107

def to_utf8
  to_s
end