Class: CryptBuffer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
CryptBufferConcern::ByteExpander, CryptBufferConcern::ByteManipulation, CryptBufferConcern::Comparable, CryptBufferConcern::Convertable, CryptBufferConcern::PrettyPrint, CryptBufferConcern::Xor, Enumerable
Defined in:
lib/crypto-toolbox/crypt_buffer.rb

Defined Under Namespace

Classes: OutOfRangeError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CryptBufferConcern::PrettyPrint

#pp

Methods included from CryptBufferConcern::ByteManipulation

#add, #mod_sub, #modulus, #sub

Methods included from CryptBufferConcern::Xor

#xor, #xor_all_with, #xor_at, #xor_space

Methods included from CryptBufferConcern::Comparable

#==

Methods included from CryptBufferConcern::Convertable

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

Constructor Details

#initialize(input) ⇒ CryptBuffer

Returns a new instance of CryptBuffer.



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

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

Instance Attribute Details

#bytesObject Also known as: b

Returns the value of attribute bytes.



27
28
29
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 27

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



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

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

.pad_hex_char(str) ⇒ Object



99
100
101
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 99

def self.pad_hex_char(str)
  (str.length == 1) ? "0#{str}" : "#{str}"
end

Instance Method Details

#chunks_of(n) ⇒ Object



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

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

#each(&block) ⇒ Object



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

def each(&block)
  @bytes.each(&block)
end

#nth_bits(n) ⇒ Object

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

Raises:



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

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