Class: Virgil::SDK::HighLevel::VirgilBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/virgil/sdk/high_level/virgil_buffer.rb

Overview

This class provides a list of methods that simplify the work with an array of bytes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes) ⇒ VirgilBuffer

Initializes a new instance of the Virgil::SDK::HighLevel::VirgilBuffer class.

Parameters:

  • bytes (Crypto::Bytes)


51
52
53
54
55
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 51

def initialize(bytes)

  self.class.validate_bytes_param(bytes)
  @bytes = bytes
end

Instance Attribute Details

#bytesCrypto::Bytes

Returns The array of raw bytes

Returns:

  • (Crypto::Bytes)

    The array of raw bytes



46
47
48
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 46

def bytes
  @bytes
end

Class Method Details

.from_base64(str) ⇒ Object

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



122
123
124
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 122

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

.from_bytes(bytes) ⇒ Object

Initializes a new buffer from array of bytes

Parameters:

  • bytes (Crypto::Bytes)


60
61
62
63
64
65
66
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 60

def self.from_bytes(bytes)

  self.validate_bytes_param(bytes)

  new(bytes)

end

.from_file(key_file_path) ⇒ Object

Initializes a new buffer from file.

Raises:

  • (ArgumentError)


114
115
116
117
118
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 114

def self.from_file(key_file_path)
  raise ArgumentError.new("file_path is not valide") unless (File.exist?(key_file_path) && File.readable?(key_file_path))
  str = File.binread(key_file_path)
  new(str.bytes)
end

.from_hex(str) ⇒ Object

Initializes a new buffer from specified string, which encodes binary data as hexadecimal digits.



134
135
136
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 134

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

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

Initializes a new buffer 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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 72

def self.from_string(str, encoding=VirgilStringEncoding::UTF8)

  case encoding
    when VirgilStringEncoding::BASE64
      return self.from_base64(str)
    when VirgilStringEncoding::HEX
      return self.from_hex(str)
    when VirgilStringEncoding::UTF8
      return self.from_utf8(str)
    else
      raise ArgumentError.new("encoding is undefined")
  end

end

.from_utf8(str) ⇒ Object

Initializes a new buffer from specified string, which encodes binary data as utf8.



128
129
130
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 128

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

Instance Method Details

#to_base64Object

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



141
142
143
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 141

def to_base64
  Base64.strict_encode64(to_s)
end

#to_hexObject

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



154
155
156
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 154

def to_hex
  to_s.each_byte.map { |b| b.to_s(16) }.join
end

#to_sObject

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



109
110
111
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 109

def to_s
  bytes.pack('c*')
end

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

Converts all the bytes in current buffer to its equivalent string representation that is encoded with selected 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



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 94

def to_string(encoding=VirgilStringEncoding::UTF8)
  case encoding
    when VirgilStringEncoding::BASE64
      return self.to_base64
    when VirgilStringEncoding::HEX
      return self.to_hex
    when VirgilStringEncoding::UTF8
      return to_s
    else
      raise ArgumentError.new("encoding is undefined")
  end
end

#to_utf8Object

Encodes all the bytes in current buffer into a utf8 string.



147
148
149
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 147

def to_utf8
  to_s
end