Class: CryptBuffer

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/crypto-toolbox/crypt_buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ CryptBuffer

Returns a new instance of CryptBuffer.



9
10
11
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 9

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

Instance Attribute Details

#bytesObject Also known as: b

Returns the value of attribute bytes.



5
6
7
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 5

def bytes
  @bytes
end

Instance Method Details

#==(other) ⇒ Object



84
85
86
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 84

def ==(other)
  bytes == bytes_from_any(other)
end

#add(n, mod: 256, offset: 0) ⇒ Object



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

def add(n, mod: 256, offset: 0)
  real_mod = [256,mod].min

  tmp = bytes.map do |b|
    val = (b + n) % real_mod
    val >= offset ? val : val+offset
  end
  CryptBuffer(tmp)
end

#bitsObject



34
35
36
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 34

def bits
  map{|b| "%08d" % b.to_s(2) }
end

#charsObject Also known as: c



24
25
26
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 24

def chars
  map{|b| b.to_i.chr}
end

#each(&block) ⇒ Object



13
14
15
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 13

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

#hexObject Also known as: h



19
20
21
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 19

def hex
  bytes2hex(bytes).upcase
end

#mod_sub(n, mod: 256) ⇒ Object



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

def mod_sub(n,mod: 256)
  tmp = bytes.map do |byte|
    val = byte.to_bn.mod_sub(n,mod).to_i
  end
  CryptBuffer(tmp)
end

#modulus(mod) ⇒ Object



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

def modulus(mod)
  real_mod = sanitize_modulus(mod)
  CryptBuffer( bytes.map{|b| b % real_mod } )
end

#ppObject



76
77
78
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 76

def pp
  puts pretty_hexstr
end

#strObject Also known as: s



29
30
31
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 29

def str
  chars.join
end

#sub(n) ⇒ Object



51
52
53
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 51

def sub(n)
  CryptBuffer( bytes.map{|byte| byte -n } )
end

#to_sObject



88
89
90
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 88

def to_s
  str
end

#xor(input, expand_input: false) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 63

def xor(input,expand_input: false)
  if expand_input
    xor_all_with(input)
  else
    xor_bytes(bytes_from_any(input))
  end
end

#xor_all_with(input) ⇒ Object



71
72
73
74
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 71

def xor_all_with(input)
  expanded = expand_bytes(bytes_from_any(input),self.bytes.length)
  xor_bytes(expanded)
end

#xor_spaceObject



80
81
82
# File 'lib/crypto-toolbox/crypt_buffer.rb', line 80

def xor_space
  xor(0x20,expand_input: true)
end