Module: Pindo::AESHelper

Defined in:
lib/pindo/base/aeshelper.rb

Class Method Summary collapse

Class Method Details

.aes_128_ecb_decrypt(key, decrypted_string) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/pindo/base/aeshelper.rb', line 70

def self.aes_128_ecb_decrypt(key, decrypted_string)
  cipher = OpenSSL::Cipher.new("AES-128-ECB")
  cipher.decrypt
  cipher.key = key
  text = cipher.update(Base64.strict_decode64(decrypted_string)) + cipher.final
  return text
end

.aes_128_ecb_encrypt(key, encrypted_string) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/pindo/base/aeshelper.rb', line 61

def self.aes_128_ecb_encrypt(key, encrypted_string)
  cipher = OpenSSL::Cipher::Cipher.new('AES-128-ECB')
  cipher.encrypt
  cipher.key = key
  txt = cipher.update(encrypted_string) << cipher.final
  content =  Base64.strict_encode64(txt)
  return content
end

.decrypt_specific_file(src_file: nil, password: nil, output_dir: nil, hash_algorithm: "MD5") ⇒ Object

The encryption parameters in this implementations reflect the old behavior which depended on the users’ local OpenSSL version 1.0.x OpenSSL and earlier versions use MD5, 1.1.0c and newer uses SHA256, we try both before giving an error



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pindo/base/aeshelper.rb', line 93

def self.decrypt_specific_file(src_file: nil, password: nil, output_dir: nil, hash_algorithm: "MD5")

  begin
    destfile = File.join(output_dir, File.basename(src_file))
    e = Match::Encryption::MatchFileEncryption.new
    e.decrypt(file_path: src_file, password: password, output_path:destfile)
    return destfile
  rescue => error
      Funlog.instance.fancyinfo_error("解析文件失败: #{src_file}")
      raise Informative, error
      return nil
  end
end

.delete_password(keychain_name: nil) ⇒ Object



50
51
52
53
# File 'lib/pindo/base/aeshelper.rb', line 50

def self.delete_password(keychain_name:nil)
  server_name = ["match", keychain_name].join("_")
  Security::InternetPassword.delete(server:server_name)
end

.encrypt_specific_file(src_file: nil, password: nil, output_dir: nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pindo/base/aeshelper.rb', line 79

def self.encrypt_specific_file(src_file: nil, password: nil, output_dir: nil)
  UI.user_error!("No password supplied") if password.to_s.strip.length == 0

  destfile = File.join(output_dir, File.basename(src_file))
  e = Match::Encryption::MatchFileEncryption.new
  e.encrypt(file_path: src_file, password: password, output_path:destfile)
  return destfile
rescue error
  puts path
  raise Informative, error
end

.fetch_password(keychain_name: nil, test_file: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pindo/base/aeshelper.rb', line 11

def self.fetch_password(keychain_name:nil, test_file:nil)
  # password = ENV["MATCH_PASSWORD"]

  server_name = ["match", keychain_name].join("_")

  item = Security::InternetPassword.find(server: server_name)

  password = item.password if item

  unless password
      puts "\e[33m[DEBUG] Keychain中未找到密码,需要用户输入: #{server_name}\e[0m" if ENV['PINDO_DEBUG']
      password = FastlaneCore::Helper.ask_password(message: "请输入证书仓库的加密密码: ", confirm: true)

      # 尝试添加密码到Keychain
      begin
        # 先尝试删除旧密码(如果存在)
        begin
          Security::InternetPassword.delete(server: server_name)
          puts "\e[33m[DEBUG] 删除Keychain中的旧密码项: #{server_name}\e[0m" if ENV['PINDO_DEBUG']
        rescue => delete_error
          # 如果不存在就忽略删除错误
          puts "\e[33m[DEBUG] 旧密码项不存在或删除失败: #{delete_error.message}\e[0m" if ENV['PINDO_DEBUG']
        end

        # 添加新密码到Keychain
        Security::InternetPassword.add(server_name, "", password)
        puts "\e[32m✓ 密码已保存到Keychain: #{server_name}\e[0m"
      rescue => e
        # 如果保存失败,警告用户但不影响继续使用
        puts "\e[31m⚠ 密码保存到Keychain失败: #{e.message}\e[0m"
        puts "\e[31m⚠ 下次使用时需要重新输入密码\e[0m"
      end
  else
      puts "\e[32m[DEBUG] 从Keychain获取密码成功: #{server_name}\e[0m" if ENV['PINDO_DEBUG']
  end

  return password
end

.store_password(keychain_name: nil, password: nil) ⇒ Object



55
56
57
58
# File 'lib/pindo/base/aeshelper.rb', line 55

def self.store_password(keychain_name:nil, password:nil)
  server_name = ["match", keychain_name].join("_")
  Security::InternetPassword.add(server_name, "", password)
end