Module: DCase::Table
Instance Method Summary collapse
- #get_table(key) ⇒ Object
- #merge(left, right, comparison) ⇒ Object
- #merge_sort(array, comparison) ⇒ Object
- #translate(table, buf) ⇒ Object
Methods included from Ext
Instance Method Details
#get_table(key) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/dcase/table.rb', line 24 def get_table key table = Array.new(256, 0) decrypt_table = Array.new(256, 0) a = Digest::MD5.digest(key).unpack('Q<')[0] i = 0 while i < 256 table[i] = i i += 1 end i = 1 while i < 1024 table = merge_sort(table, lambda { |x, y| a % (x + i) - a % (y + i) }) i += 1 end i = 0 while i < 256 decrypt_table[table[i]] = i i += 1 end [ table, decrypt_table ] end |
#merge(left, right, comparison) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/dcase/table.rb', line 64 def merge (left, right, comparison) result = [] while (left.length > 0) and (right.length > 0) if comparison.call(left[0], right[0]) <= 0 result.push left.shift() else result.push right.shift() end end result.push left.shift() while left.length > 0 result.push right.shift() while right.length > 0 result end |
#merge_sort(array, comparison) ⇒ Object
78 79 80 81 82 |
# File 'lib/dcase/table.rb', line 78 def merge_sort (array, comparison) return array if array.size < 2 middle = (array.size / 2).ceil merge(merge_sort(array.slice(0, middle), comparison), merge_sort(array.slice(middle .. array.size), comparison), comparison) end |
#translate(table, buf) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/dcase/table.rb', line 51 def translate(table, buf) table_ptr = FFI::MemoryPointer.new(:int, table.length) table_ptr.put_array_of_int32(0, table) buf_ptr = FFI::MemoryPointer.new(:string, buf.length) buf_ptr.put_bytes(0, buf) r = Ext.encrypt(table_ptr, buf_ptr, buf.length).get_bytes(0, buf.length) table_ptr.free buf_ptr.free r end |