Sha256: 10408e8c3192b717366e7c5e7b3d31601b76583544a5797aa138957cfc124b71
Contents?: true
Size: 1.69 KB
Versions: 2
Compression:
Stored size: 1.69 KB
Contents
module EventMachine module WebSocket class MaskedString < String # Read a 4 bit XOR mask - further requested bytes will be unmasked def read_mask if respond_to?(:encoding) && encoding.name != "ASCII-8BIT" raise "MaskedString only operates on BINARY strings" end raise "Too short" if bytesize < 4 # TODO - change @masking_key = String.new(self[0..3]) end def self.create_mask MaskedString.new "rAnD" #TODO make random 4 character string end def self.create_masked_string(original) masked_string = MaskedString.new masking_key = self.create_mask masked_string << masking_key original.size.times do |i| char = original.getbyte(i) masked_string << (char ^ masking_key.getbyte(i%4)) end if masked_string.respond_to?(:force_encoding) masked_string.force_encoding("ASCII-8BIT") end masked_string.read_mask # get input string return masked_string end # Removes the mask, behaves like a normal string again def unset_mask @masking_key = nil end def slice_mask slice!(0, 4) end def getbyte(index) if @masking_key masked_char = super masked_char ? masked_char ^ @masking_key.getbyte(index % 4) : nil else super end end def getbytes(start_index, count) data = '' if @masking_key count.times do |i| data << getbyte(start_index + i) end else data = String.new(self[start_index..start_index+count]) end data end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
sonixlabs-em-websocket-0.3.8 | lib/em-websocket/masking04.rb |
sonixlabs-em-websocket-0.3.7 | lib/em-websocket/masking04.rb |