Module: Jekyll::EncFilter
- Included in:
- EncFilterClass, Test
- Defined in:
- lib/enc.rb
Class Method Summary collapse
Instance Method Summary collapse
- #bin2hex(str) ⇒ Object
- #encrypt(msg, password) ⇒ Object
- #encrypt_content(content, page, prefix) ⇒ Object
- #encrypt_content_v2(content, pswHex) ⇒ Object
- #encrypt_key(x, page, keyHex2Enc, encid) ⇒ Object
- #gen_test_data_forkey(pswHex) ⇒ Object
- #genKey(password) ⇒ Object
- #get_encrypt_id(content, page) ⇒ Object
- #rand_bytes(_, n2) ⇒ Object
- #write_file(new_content, file_path) ⇒ Object
Class Method Details
.hex2bin(str) ⇒ Object
110 111 112 |
# File 'lib/enc.rb', line 110 def self.hex2bin(str) [str].pack "H*" end |
Instance Method Details
#bin2hex(str) ⇒ Object
106 107 108 |
# File 'lib/enc.rb', line 106 def bin2hex(str) str.unpack('C*').map{ |b| "%02x" % b }.join('') end |
#encrypt(msg, password) ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/enc.rb', line 127 def encrypt(msg,password) cipher = OpenSSL::Cipher::AES.new(256, :GCM).encrypt iv = cipher.random_iv cipher.iv = iv cipher.key = genKey password encrypted = cipher.update(msg) + cipher.final return 'E2.' + Base64.strict_encode64(iv + encrypted + cipher.auth_tag) end |
#encrypt_content(content, page, prefix) ⇒ Object
145 146 147 148 149 |
# File 'lib/enc.rb', line 145 def encrypt_content(content,page,prefix) psw = EncFilterTool.getKey(content,page) psw = prefix + psw + prefix return encrypt content,psw end |
#encrypt_content_v2(content, pswHex) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/enc.rb', line 180 def encrypt_content_v2(content,pswHex) if !pswHex || pswHex.length != 64 raise "invalid Key:" + pswHex end cipher = OpenSSL::Cipher::AES.new(256, :CTR).encrypt iv = cipher.random_iv cipher.iv = iv cipher.key = EncFilter.hex2bin(pswHex) encrypted = cipher.update(content) + cipher.final len = 4 + iv.length + encrypted.length lenBf = Jekyll.nmberToBinary4(len) lenBf2 = lenBf.bytes.map.each_with_index do |v,i| z = i ^ iv.bytes[i ] v ^ z end.pack('C*') return Base64.strict_encode64(lenBf2 + iv + encrypted ) end |
#encrypt_key(x, page, keyHex2Enc, encid) ⇒ Object
200 201 202 203 204 205 206 207 208 |
# File 'lib/enc.rb', line 200 def encrypt_key(x,page,keyHex2Enc,encid) arr = EncFilterTool.getAllKey(page) newArr = arr.map do |k| key = genKey encid + k + encid hexKey = bin2hex key Base64.strict_decode64(encrypt_content_v2(EncFilter.hex2bin(keyHex2Enc),hexKey)) end Base64.strict_encode64(newArr.join) end |
#gen_test_data_forkey(pswHex) ⇒ Object
209 210 211 212 213 214 |
# File 'lib/enc.rb', line 209 def gen_test_data_forkey(pswHex) rndBf = Base64.strict_encode64(EncFilter.hex2bin(rand_bytes('',63))) encData = encrypt_content_v2(rndBf,pswHex) rndBf + '.' + encData end |
#genKey(password) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/enc.rb', line 113 def genKey(password) cacheKey = $KeyMap[password] if cacheKey return cacheKey end salt = 'this is a salt string 20221019' iter = 12345 key_len = 32 key = OpenSSL::KDF.pbkdf2_hmac(password, salt: salt, iterations: iter, length: key_len, hash: "sha256") $KeyMap[password] = key return key end |
#get_encrypt_id(content, page) ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/enc.rb', line 136 def get_encrypt_id(content,page) key = EncFilterTool.getKey(content,page) if key != nil && key.length > 0 enckey = genKey(key).unpack('H*').first return OpenSSL::HMAC.hexdigest("SHA256", "no-style-please2-key-digst-2022-05-21", key.to_s + enckey)[0..32] else return "" end end |
#rand_bytes(_, n2) ⇒ Object
175 176 177 |
# File 'lib/enc.rb', line 175 def rand_bytes(_,n2) return bin2hex(OpenSSL::Random.random_bytes(n2)) end |
#write_file(new_content, file_path) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/enc.rb', line 151 def write_file(new_content,file_path) dirpath = File.dirname(file_path) File.dirname(dirpath) if File.exist?(file_path) # 读取现有文件内容 current_content = File.read(file_path) # 如果内容不同,则写入新内容 unless current_content == new_content File.write(file_path, new_content) puts "write file" + file_path else puts "write file: same, skip " + file_path end else # 文件不存在,直接写入 File.write(file_path, new_content) puts "write file" + file_path end return '' end |