Class: CommonEnc

Inherits:
Object
  • Object
show all
Defined in:
lib/common_enc.rb

Direct Known Subclasses

ECStr, EJStr

Constant Summary collapse

CSTR_DICT =
[28, 37, 14, 120, 35, 12, 254, 46, 88, 78, 98, 99, 7, 8, 24, 9, 25, 12, 2, 201, 145]
HEX_DICT =
['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files) ⇒ CommonEnc

Returns a new instance of CommonEnc.



13
14
15
16
# File 'lib/common_enc.rb', line 13

def initialize(files)
  @files = files
  @@single_instance = self
end

Class Method Details

.decrypt(instr) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/common_enc.rb', line 59

def self.decrypt(instr)
  mid = []
  (instr.length/2).times do |i|
    v = (instr[i*2].to_i(16) << 4) | instr[i*2+1].to_i(16)
    mid << ( v^CSTR_DICT[i%CSTR_DICT.length] )
  end
  mid.pack("c"*mid.length)
end

.get_instanceObject



68
69
70
# File 'lib/common_enc.rb', line 68

def self.get_instance
  @@single_instance
end

Instance Method Details

#_(str) ⇒ Object

function used in erb file



77
78
79
80
# File 'lib/common_enc.rb', line 77

def _(str)
  id = add(str_to_id(str), str)
  return "#{getstr_funcname}(#{id})"
end

#add(id, str) ⇒ Object

add an new item id<->str



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/common_enc.rb', line 35

def add(id, str)
  @dict = Hash.new if @dict.nil?
  newid = id
  if @dict.has_key? id
    if @dict[id] != str
      newid = (id.to_s + "_1").intern
      return add newid, str
    end
    return id
  end
  
  @dict[id] = str
  return id
end

#dictObject

for debug



51
52
53
# File 'lib/common_enc.rb', line 51

def dict
  @dict
end

#encrypt(instr) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/common_enc.rb', line 18

def encrypt(instr)
  rst = ""
  idx = 0
  instr.each_byte do |b| 
    v = CSTR_DICT[idx%CSTR_DICT.length]^b
    rst += HEX_DICT[v>>4]
    rst += HEX_DICT[v&0xf]
    idx += 1 
  end
  return rst
end

#filesObject



55
56
57
# File 'lib/common_enc.rb', line 55

def files
  @files
end

#gem_root_dirObject



9
10
11
# File 'lib/common_enc.rb', line 9

def gem_root_dir
  File.absolute_path(File.join(File.dirname(File.expand_path(__FILE__)), ".."))
end

#getstr_funcnameObject



72
73
74
# File 'lib/common_enc.rb', line 72

def getstr_funcname
  "_"
end

#str_to_id(str) ⇒ Object



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

def str_to_id(str) 
  Digest::SHA256.bubblebabble(Digest::SHA256.digest(str)).upcase.gsub('-','_').intern
end