Skip to content

Instantly share code, notes, and snippets.

@rxbit
Created March 3, 2016 14:22
Show Gist options
  • Save rxbit/0119c53f27f316b786a0 to your computer and use it in GitHub Desktop.
Save rxbit/0119c53f27f316b786a0 to your computer and use it in GitHub Desktop.
require 'ffi'
module TEA
extend FFI::Library
ffi_lib 'tea'
attach_function :tea_encode, :TEAencode, [:pointer, :uint, :pointer, :pointer, :pointer], :int
attach_function :tea_decode, :TEAdecode, [:pointer, :uint, :pointer, :pointer, :pointer], :int
def self.encode(data, key)
insize = data.bytesize
inBuf = FFI::MemoryPointer.new(:uchar, insize)
inBuf.write_string(data, insize)
keyBuf = FFI::MemoryPointer.new(:uint, 4)
keyBuf.write_string(key, 16)
outBuf = FFI::MemoryPointer.new(:char, insize+8)
outSize = FFI::MemoryPointer.new(:uint, 1)
outSize.write_uint insize+8
ret = tea_encode(inBuf,insize,outBuf,outSize,keyBuf)
(ret == 0) ? outBuf.read_string(outSize.read_uint) : nil
end
def self.decode(data, key)
insize = data.bytesize
inBuf = FFI::MemoryPointer.new(:uchar, insize)
inBuf.write_string(data, insize)
keyBuf = FFI::MemoryPointer.new(:uint, 4)
keyBuf.write_string(key, 16)
outBuf = FFI::MemoryPointer.new(:char, insize+8)
outSize = FFI::MemoryPointer.new(:uint, 1)
outSize.write_uint insize+8
ret = tea_decode(inBuf,insize,outBuf,outSize,keyBuf)
(ret == 0) ? outBuf.read_string(outSize.read_uint) : nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment