Module: Omnizip::Password

Defined in:
lib/omnizip/password.rb,
lib/omnizip/password/password_validator.rb,
lib/omnizip/password/encryption_registry.rb,
lib/omnizip/password/encryption_strategy.rb,
lib/omnizip/password/winzip_aes_strategy.rb,
lib/omnizip/password/zip_crypto_strategy.rb

Overview

Password protection module Provides encryption and password validation for archives

Defined Under Namespace

Classes: EncryptionRegistry, EncryptionStrategy, PasswordValidator, WinzipAesStrategy, ZipCryptoStrategy

Class Method Summary collapse

Class Method Details

.create_strategy(method, password, **options) ⇒ EncryptionStrategy

Create encryption strategy

Parameters:

  • method (Symbol)

    Encryption method

  • password (String)

    Password

  • options (Hash)

    Strategy options

Returns:



37
38
39
# File 'lib/omnizip/password.rb', line 37

def create_strategy(method, password, **options)
  EncryptionRegistry.create(method, password, **options)
end

.encryption_methodsArray<Symbol>

Get available encryption methods

Returns:

  • (Array<Symbol>)

    List of methods



43
44
45
# File 'lib/omnizip/password.rb', line 43

def encryption_methods
  EncryptionRegistry.strategies
end

.from_env(var_name = "OMNIZIP_PASSWORD") ⇒ String?

Read password from environment variable

Parameters:

  • var_name (String) (defaults to: "OMNIZIP_PASSWORD")

    Environment variable name

Returns:

  • (String, nil)

    Password or nil



73
74
75
# File 'lib/omnizip/password.rb', line 73

def from_env(var_name = "OMNIZIP_PASSWORD")
  ENV.fetch(var_name, nil)
end

.from_file(file_path) ⇒ String

Read password from file

Parameters:

  • file_path (String)

    Path to password file

Returns:

  • (String)

    Password from file



80
81
82
# File 'lib/omnizip/password.rb', line 80

def from_file(file_path)
  File.read(file_path).strip
end

.prompt(confirm: true) ⇒ String

Prompt for password (with confirmation)

Parameters:

  • confirm (Boolean) (defaults to: true)

    Require confirmation

Returns:

  • (String)

    Password entered by user



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/omnizip/password.rb', line 50

def prompt(confirm: true)
  require "io/console"

  print "Enter password: "
  password = $stdin.noecho(&:gets).chomp
  puts

  if confirm
    print "Confirm password: "
    confirmation = $stdin.noecho(&:gets).chomp
    puts

    unless password == confirmation
      raise PasswordError, "Passwords do not match"
    end
  end

  password
end

.strength(password) ⇒ Symbol

Check password strength

Parameters:

  • password (String)

    Password to check

Returns:

  • (Symbol)

    Strength label (:weak, :fair, :good, :strong)



27
28
29
30
# File 'lib/omnizip/password.rb', line 27

def strength(password)
  validator = PasswordValidator.new
  validator.strength_label(password)
end

.validate(password, **options) ⇒ Boolean

Validate a password

Parameters:

  • password (String)

    Password to validate

  • options (Hash)

    Validation options

Returns:

  • (Boolean)

    True if valid



19
20
21
22
# File 'lib/omnizip/password.rb', line 19

def validate(password, **options)
  validator = PasswordValidator.new(**options)
  validator.validate(password)
end