Class: CryptBuffer

Defined Under Namespace

Classes: OutOfRangeError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CryptBufferConcern::Xor

#xor, #xor_all_with, #xor_at, #xor_space

Methods included from CryptBufferConcern::Random

included

Methods included from CryptBufferConcern::PrettyPrint

#pp

Methods included from CryptBufferConcern::Padding

#pad, #padding, #padding?, #strip_padding

Methods included from CryptBufferConcern::Comparable

#==

Methods included from CryptBufferConcern::Convertable

#bits, #chars, #hex, #str, #to_s

Methods included from CryptBufferConcern::Arithmetic

#add, #mod_sub, #modulus, #sub

Constructor Details

#initialize(input) ⇒ CryptBuffer

Returns a new instance of CryptBuffer.



38
39
40
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 38

def initialize(input)
  @bytes = bytes_from_any(input)
end

Instance Attribute Details

#bytesObject Also known as: b

Returns the value of attribute bytes.



32
33
34
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 32

def bytes
  @bytes
end

Class Method Details

.from_hex(input) ⇒ Object

Make sure input strings are always interpreted as hex strings This is especially useful for unknown or uncertain inputs like strings with or without leading 0x



45
46
47
48
49
50
51
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 45

def self.from_hex(input)
  hexstr =""
  unless input.nil?
    hexstr = (input =~ /^0x/ ? input : "0x#{pad_hex_char(input)}" )
  end
  CryptBuffer.new(hexstr)
end

Instance Method Details

#chunks_of(n) ⇒ Object



61
62
63
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 61

def chunks_of(n)
  self.bytes.each_slice(n).map{|chunk| CryptBuffer(chunk) }
end

#nth_bits(n) ⇒ Object

Returns an array of the nth least sigificant by bit of each byte

Raises:



54
55
56
57
58
59
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 54

def nth_bits(n)
  raise OutOfRangeError if n < 0
  raise OutOfRangeError if n > 7
  
  bits.map{|b| b.reverse[n].to_i }
end