Class: Encrypted::ByteStream

Inherits:
String
  • Object
show all
Defined in:
lib/encrypted/bytestream.rb

Constant Summary collapse

Use_getbyte =
"".respond_to?(:getbyte)
@@strict_mode =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.strict_mode=(new_mode) ⇒ Object



56
57
58
# File 'lib/encrypted/bytestream.rb', line 56

def self.strict_mode=(new_mode)
  @@strict_mode=new_mode
end

Instance Method Details

#+(string) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/encrypted/bytestream.rb', line 30

def +(string)
    my_dwords=self.unpack("N*")
    other_dwords=string.unpack("N*")
    max_length=if(my_dwords.length > other_dwords.length)
        my_dwords.length
    else
        other_dwords.length
    end
    out_dwords=Array.new
    0.upto(max_length-1) do
        |i|
        out_dwords[i]=(my_dwords[i]||0)+(other_dwords[i]||0)&0xffffffff
    end
    self.class.new(out_dwords.pack("N*"))
end

#[](anything, whatever = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/encrypted/bytestream.rb', line 59

def [](anything, whatever=nil)
  if(whatever)
    super(anything, whatever)
  elsif(not anything.is_a? Numeric)
    super(anything)
  elsif(@@strict_mode)
    raise "Ambiguous, you must use #byte_at instead"
  else
    STDERR.puts "Ambiguous usage of [], please use #byte_at"
  end
end

#^(string) ⇒ Object

Returned values are still of class Encrypted::ByteStream, and are of the same length as the longer value. Note that this means that: a=Encrypted::ByteStream.new(“a”) bb=Encrypted::ByteStream.new(“b”) a^bb^bb

…does not equal “a” but rather “a000”, so this should be used with caution except where you have equal-size strings in mind.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/encrypted/bytestream.rb', line 15

def ^(string)
    max_length=if(self.length > string.length)
        self.length
    else
        string.length
    end
    my_bytes=self.unpack("C*")
    other_bytes=string.unpack("C*")
    out_bytes=Array.new
    0.upto(max_length-1) do
        |i|
        out_bytes[i]=(my_bytes[i]||0)^(other_bytes[i]||0)
    end
    self.class.new(out_bytes.pack("C*"))
end

#byte_at(position, new_value = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/encrypted/bytestream.rb', line 46

def byte_at(position, new_value=nil)
  if(new_value)
    self[position, 1]=[new_value].pack("C")
  elsif(Use_getbyte)
    self.getbyte(position)
  else
    self.slice(position)
  end
end