encstr 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 796dddde8df6deecbf2cdadaf252405574588dac
4
+ data.tar.gz: 4103094efa5c975da9d97694c302453e0da7a21b
5
+ SHA512:
6
+ metadata.gz: bc98b55e1528f680f83a3b6e3073eb87ead65da7676b257cca4ac2a2c49120b83a0b265f7de3bbbbefe49b58532478b8b628ad240ad70197a52d396fcb1c7c03
7
+ data.tar.gz: 3b10b7f86eed7733883f1ed9fe18b912ef4beec5f38867160c6ba56876f3d53393cacb44996422e320b5c8aa2753e580cf15670857a6fde12174a5839487bad9
data/bin/ess ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.dirname(File.dirname(File.realpath(__FILE__)))+"/lib")
4
+ require 'ess'
5
+
6
+ ESS.start(ARGV)
data/lib/common_enc.rb ADDED
@@ -0,0 +1,82 @@
1
+ require 'digest'
2
+ require 'digest/bubblebabble'
3
+ require 'erb'
4
+
5
+ class CommonEnc
6
+ CSTR_DICT = [28, 37, 14, 120, 35, 12, 254, 46, 88, 78, 98, 99, 7, 8, 24, 9, 25, 12, 2, 201, 145]
7
+ HEX_DICT = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']
8
+
9
+ def gem_root_dir
10
+ File.absolute_path(File.join(File.dirname(File.expand_path(__FILE__)), ".."))
11
+ end
12
+
13
+ def initialize(files)
14
+ @files = files
15
+ @@single_instance = self
16
+ end
17
+
18
+ def encrypt(instr)
19
+ rst = ""
20
+ idx = 0
21
+ instr.each_byte do |b|
22
+ v = CSTR_DICT[idx%CSTR_DICT.length]^b
23
+ rst += HEX_DICT[v>>4]
24
+ rst += HEX_DICT[v&0xf]
25
+ idx += 1
26
+ end
27
+ return rst
28
+ end
29
+
30
+ def str_to_id(str)
31
+ Digest::SHA256.bubblebabble(Digest::SHA256.digest(str)).upcase.gsub('-','_').intern
32
+ end
33
+
34
+ # add an new item id<->str
35
+ def add(id, str)
36
+ @dict = Hash.new if @dict.nil?
37
+ newid = id
38
+ if @dict.has_key? id
39
+ if @dict[id] != str
40
+ newid = (id.to_s + "_1").intern
41
+ return add newid, str
42
+ end
43
+ return id
44
+ end
45
+
46
+ @dict[id] = str
47
+ return id
48
+ end
49
+
50
+ # for debug
51
+ def dict
52
+ @dict
53
+ end
54
+
55
+ def files
56
+ @files
57
+ end
58
+
59
+ def self.decrypt(instr)
60
+ mid = []
61
+ (instr.length/2).times do |i|
62
+ v = (instr[i*2].to_i(16) << 4) | instr[i*2+1].to_i(16)
63
+ mid << ( v^CSTR_DICT[i%CSTR_DICT.length] )
64
+ end
65
+ mid.pack("c"*mid.length)
66
+ end
67
+
68
+ def self.get_instance
69
+ @@single_instance
70
+ end
71
+
72
+ def getstr_funcname
73
+ "_"
74
+ end
75
+
76
+ # function used in erb file
77
+ def _(str)
78
+ id = add(str_to_id(str), str)
79
+ return "#{getstr_funcname}(#{id})"
80
+ end
81
+
82
+ end
data/lib/ecstr.rb ADDED
@@ -0,0 +1,53 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'common_enc'
3
+
4
+ # TODO:
5
+ # 1. create gtest code
6
+ # 2. specify output encrypt c string filename
7
+
8
+ =begin
9
+ Usage:
10
+ ruby -I . ecstr.rb a.cpp.erb b.c.erb c.cpp.erb
11
+
12
+ Result:
13
+ create a.cpp, b.c, c.cpp, ecstrdef.data
14
+
15
+ In the erb file,when using a string, write as:
16
+
17
+ <%= _ "the string which will be encrypted" %>
18
+
19
+ Remember to <% require 'ecstr' %> at begining of the erb file.
20
+ =end
21
+
22
+ class ECStr < CommonEnc
23
+
24
+ DATA_FILE_NAME = "ecstrdef.data"
25
+
26
+ def run
27
+ @dict = Hash.new
28
+ b = binding
29
+
30
+ @files.each do |infile|
31
+ outfile = infile[0..-5] # "a.cpp.erb" => "a.cpp"
32
+ erbfile = ERB.new(File.read infile)
33
+ File.write outfile, erbfile.result(b)
34
+ print "#{infile} --> #{outfile}\n"
35
+ end
36
+
37
+ ids = "enum {\n __BEGIN = -1,\n"
38
+ strs = "static const char *estrs[] = {\n"
39
+
40
+ @dict.each do |key, value|
41
+ ids += " #{key},\n"
42
+ strs += " \"#{encrypt value}\",\n"
43
+ end
44
+
45
+ ids += " ENCRYPTED_STR_TOTAL_NUM\n};\n"
46
+ strs += " NULL\n};\n"
47
+ File.write DATA_FILE_NAME, "#ifndef __#{DATA_FILE_NAME.gsub('.','_').upcase}__\n#define __#{DATA_FILE_NAME.gsub('.','_').upcase}__\n\n#{ids}\n\n#{strs}\n\n#endif\n"
48
+ print "Data file #{DATA_FILE_NAME} created.\n"
49
+ end
50
+
51
+ end
52
+
53
+
data/lib/ejstr.rb ADDED
@@ -0,0 +1,51 @@
1
+
2
+
3
+ class EJStr < CommonEnc
4
+ JAVA_TEMPLATE_FILE = "ejstr.java.erb"
5
+
6
+ def getstr_funcname
7
+ "#{@@package_name}.#{@@class_name}._"
8
+ end
9
+
10
+ def initialize(files, pkg_name, clz_name)
11
+ super(files)
12
+ @@package_name = pkg_name
13
+ @@class_name = clz_name
14
+ end
15
+
16
+ def run
17
+ @dict = Hash.new
18
+ b = binding
19
+
20
+ @files.each do |infile|
21
+ outfile = infile[0..-5]
22
+ erbfile = ERB.new(File.read infile)
23
+ File.write outfile, erbfile.result(b)
24
+ print "#{infile} -> #{outfile}\n"
25
+ end
26
+
27
+ idx = 0
28
+ ids = "" #private class _ENJSTR_IDS { \n"
29
+ strs = "" #private String enc_j_strs[] = { \n"
30
+
31
+ @dict.each do |key, value|
32
+ ids += " public static final int #{key} = #{idx};\n"
33
+ idx += 1
34
+ strs += " \"#{encrypt value}\",\n"
35
+ end
36
+
37
+ ids += " public static final int ENCRYPTED_STR_TOTAL_NUM = #{idx};"
38
+ strs += " null"
39
+
40
+ str_src_file = "#{@@class_name}.java"
41
+ erbfile = ERB.new(File.read File.join(gem_root_dir, "lib/#{JAVA_TEMPLATE_FILE}"))
42
+ File.write str_src_file, erbfile.result(binding)
43
+ end
44
+
45
+ def _(str)
46
+ id = add(str_to_id(str), str)
47
+ return "#{getstr_funcname}(#{@@package_name}.#{@@class_name}._ENJSTR_IDS.#{id})"
48
+ end
49
+ end
50
+
51
+
data/lib/ess.rb ADDED
@@ -0,0 +1,24 @@
1
+ #! /usr/bin/env ruby
2
+ # -!- encoding: utf-8 -!-
3
+
4
+ require 'thor'
5
+ require 'ecstr'
6
+ require 'ejstr'
7
+
8
+ class ESS < Thor
9
+ class_option :key, :aliases => '-k', :type => :array, :banner => '加密密钥,不指定是将使用随机生成的密钥'
10
+
11
+ desc 'c [OPTIONS] FILES', '将C/C++/ObjC源文件FILES变换成使用加密数据库的源文件'
12
+ option :gtest, :default => false, :aliases => '-g', :type => :boolean, :banner => '是否生成gtest测试用例(只对c代码有效)'
13
+ option :outputdir, :default => "./", :aliases => '-o', :type => :string, :banner => '输出文件目录,默认为输入文件相同目录'
14
+ def c(*files)
15
+ ECStr.new(files).run
16
+ end
17
+
18
+ desc "java [OPTIONS] FILES", "将Java源文件变换成使用加密字符串的源文件"
19
+ option :pkg_name, :aliases => '-p', :type => :string, :banner => '生成的加密库文件所在包名', :default => "net.z4tech.enstr"
20
+ option :clz_name, :aliases => '-c', :type => :string, :banner => '生成的加密库文件字符串解密函数所在函数名', :default => "EJStr"
21
+ def java(*files)
22
+ EJStr.new(files, options[:pkg_name], options[:clz_name]).run
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: encstr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Ziv Yang
8
+ autorequire:
9
+ bindir: bin/
10
+ cert_chain: []
11
+ date: 2016-02-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This tool can pre-build c/c++/java source code and encrypt the strings
14
+ within them.
15
+ email: ammer.y@gmail.com
16
+ executables:
17
+ - ess
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/common_enc.rb
22
+ - lib/ecstr.rb
23
+ - lib/ejstr.rb
24
+ - lib/ess.rb
25
+ - bin/ess
26
+ homepage: http://host.z4tech.net/software/encstr
27
+ licenses:
28
+ - Mit
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.0.14
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: A tool to encrypt strings in java/c/c++ programs
50
+ test_files: []