Class: Virgil::Crypto::Bytes
- Inherits:
-
Array
- Object
- Array
- Virgil::Crypto::Bytes
- Defined in:
- lib/virgil/crypto/bytes.rb
Class Method Summary collapse
-
.from_base64(str) ⇒ Object
Initializes a new array of bytes from specified string, which encodes binary data as base-64 digits.
-
.from_hex(str) ⇒ Object
Initializes a new array of bytes from specified string, which encodes binary data as hexadecimal digits.
-
.from_string(str, encoding = VirgilStringEncoding::UTF8) ⇒ Object
Initializes a new array of bytes from specified string, which encodes binary data.
-
.from_utf8(str) ⇒ Object
Initializes a new array of bytes from specified string, which encodes binary data as utf8.
Instance Method Summary collapse
-
#to_base64 ⇒ Object
Converts all the bytes to its equivalent string representation that is encoded with base-64 digits.
-
#to_hex ⇒ Object
Converts the numeric value of each element of a current array of bytes to its equivalent hexadecimal string representation.
-
#to_s ⇒ Object
Converts all the bytes to its equivalent string representation in utf8.
-
#to_string(encoding = VirgilStringEncoding::UTF8) ⇒ String
Decodes the current bytes to a string according to the specified character encoding.
-
#to_utf8 ⇒ Object
Encodes all the bytes into a utf8 string.
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.
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_base64 ⇒ Object
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_hex ⇒ Object
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_s ⇒ Object
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.
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_utf8 ⇒ Object
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 |