Module: XXTEA

Extended by:
XXTEA
Included in:
XXTEA
Defined in:
lib/xxtea/xxtea_ruby.rb,
lib/xxtea.rb,
lib/xxtea/xxtea_ffi.rb,
ext/xxtea/ruby_xxtea.c

Overview

# xxtea_ruby.rb #

#

XXTEA encryption algorithm library for Ruby. #

#

Encryption Algorithm Authors: #

David J. Wheeler                                    #
Roger M. Needham                                    #
                                                    #

Code Author: Ma Bingyao <mabingyao@gmail.com> # LastModified: Feb 13, 2016 #

#

Defined Under Namespace

Modules: LIB

Constant Summary collapse

VERSION =
"1.3.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.decrypt(data, key) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'ext/xxtea/ruby_xxtea.c', line 45

VALUE rb_decrypt(VALUE mod, VALUE data, VALUE key) {
	void * result;
	VALUE retval;
	size_t data_len, out_len;

	if (data == Qnil) return rb_str_new(0,0);

	Check_Type(data, T_STRING);
	Check_Type(key, T_STRING);

	data_len = RSTRING_LEN(data);

	if (data_len == 0) return data;

	result = xxtea_decrypt(RSTRING_PTR(data), data_len, RSTRING_PTR(key), &out_len);

	if (result == NULL) return rb_str_new(0,0);

	retval = rb_str_new((const char *)result, out_len);

	free(result);

	return retval;
}

.encrypt(data, key) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'ext/xxtea/ruby_xxtea.c', line 20

VALUE rb_encrypt(VALUE mod, VALUE data, VALUE key) {
	void * result;
	VALUE retval;
	size_t data_len, out_len;

	if (data == Qnil) return rb_str_new(0,0);

	Check_Type(data, T_STRING);
	Check_Type(key, T_STRING);

	data_len = RSTRING_LEN(data);

	if (data_len == 0) return data;

	result = xxtea_encrypt(RSTRING_PTR(data), data_len, RSTRING_PTR(key), &out_len);

	if (result == NULL) return rb_str_new(0,0);

	retval = rb_str_new((const char *)result, out_len);

	free(result);

	return retval;
}

Instance Method Details

#decrypt(data, key) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/xxtea/xxtea_ffi.rb', line 51

def decrypt(data, key)
  return nil if data.nil?
  data_len = data.bytesize
  data = FFI::MemoryPointer.from_string(data)
  key = FFI::MemoryPointer.from_string(key)
  out_len = FFI::MemoryPointer.new(:size_t, 1)
  result = LIB.xxtea_decrypt(data, data_len, key, out_len)
  return nil if result.null?
  retval = result.read_bytes(out_len.read_size_t)
  LIB.free(result)
  return retval
end

#decrypt_utf8(data, key) ⇒ Object



30
31
32
# File 'lib/xxtea.rb', line 30

def decrypt_utf8(data, key)
  decrypt(data, key).force_encoding(Encoding::UTF_8)
end

#encrypt(data, key) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/xxtea/xxtea_ffi.rb', line 39

def encrypt(data, key)
  return nil if data.nil?
  data_len = data.bytesize
  data = FFI::MemoryPointer.from_string(data)
  key = FFI::MemoryPointer.from_string(key)
  out_len = FFI::MemoryPointer.new(:size_t, 1)
  result = LIB.xxtea_encrypt(data, data_len, key, out_len)
  return nil if result.null?
  retval = result.read_bytes(out_len.read_size_t)
  LIB.free(result)
  return retval
end