Ruby 语言 编写密码生成器 CLI 自定义长度 / 复杂度 / 排除易混淆字符

Ruby阿木 发布于 1 天前 3 次阅读


Ruby CLI 密码生成器:从基础到高级

随着互联网的普及和网络安全的重要性日益凸显,密码作为保护个人和机构数据安全的第一道防线,其重要性不言而喻。一个好的密码应该具备足够的复杂度和长度,同时避免使用易混淆字符。本文将围绕Ruby语言,介绍如何编写一个简单的密码生成器CLI(命令行界面),并逐步提升其功能,使其更加灵活和强大。

基础密码生成器

1. 环境准备

确保你的计算机上已经安装了Ruby。可以通过以下命令检查Ruby版本:

ruby
ruby -v

2. 编写代码

以下是一个简单的密码生成器CLI的代码示例:

ruby
!/usr/bin/env ruby

def generate_password(length, complexity, exclude_confusing_chars)
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
exclude_chars = exclude_confusing_chars.split('')
chars = chars.delete(exclude_chars.join) if exclude_confusing_chars

password = ''
length.times do
password << chars[rand(chars.size)]
end

password
end

puts 'Welcome to the Password Generator CLI!'
puts 'Please enter the desired password length: '
length = gets.to_i
puts 'Please enter the desired complexity level (1-5): '
complexity = gets.to_i
puts 'Do you want to exclude confusing characters? (yes/no): '
exclude_confusing_chars = gets.strip.downcase == 'yes' ? 'l1IoO0' : ''

password = generate_password(length, complexity, exclude_confusing_chars)
puts "Your generated password is: {password}"

3. 运行程序

将上述代码保存为 `password_generator.rb`,然后在命令行中运行:

bash
ruby password_generator.rb

按照提示输入所需的密码长度、复杂度等级以及是否排除易混淆字符,即可生成密码。

提升密码生成器功能

1. 复杂度等级

为了使密码生成器更加灵活,我们可以为不同复杂度等级定义不同的字符集。以下是一个改进后的代码示例:

ruby
def generate_password(length, complexity, exclude_confusing_chars)
case complexity
when 1
chars = 'abcdefghijklmnopqrstuvwxyz'
when 2
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
when 3
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
else
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@$%^&()_+-='
end

exclude_chars = exclude_confusing_chars.split('')
chars = chars.delete(exclude_chars.join) if exclude_confusing_chars

password = ''
length.times do
password << chars[rand(chars.size)]
end

password
end

2. 随机性增强

为了提高密码的随机性,我们可以使用更复杂的随机数生成方法。以下是一个使用`SecureRandom`模块的示例:

ruby
require 'securerandom'

def generate_password(length, complexity, exclude_confusing_chars)
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@$%^&()_+-='
exclude_chars = exclude_confusing_chars.split('')
chars = chars.delete(exclude_chars.join) if exclude_confusing_chars

password = ''
length.times do
password << chars[SecureRandom.random_number(chars.size)]
end

password
end

3. 用户界面优化

为了提高用户体验,我们可以对用户界面进行优化,例如添加错误处理和提示信息。以下是一个改进后的代码示例:

ruby
!/usr/bin/env ruby

require 'securerandom'

def generate_password(length, complexity, exclude_confusing_chars)
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@$%^&()_+-='
exclude_chars = exclude_confusing_chars.split('')
chars = chars.delete(exclude_chars.join) if exclude_confusing_chars

password = ''
length.times do
password << chars[SecureRandom.random_number(chars.size)]
end

password
end

def prompt_for_input(prompt, type)
puts prompt
value = gets.strip
case type
when :integer
value.to_i
when :boolean
value.downcase == 'yes'
else
value
end
end

puts 'Welcome to the Password Generator CLI!'
length = prompt_for_input('Please enter the desired password length: ', :integer)
complexity = prompt_for_input('Please enter the desired complexity level (1-5): ', :integer)
exclude_confusing_chars = prompt_for_input('Do you want to exclude confusing characters? (yes/no): ', :boolean) ? 'l1IoO0' : ''

password = generate_password(length, complexity, exclude_confusing_chars)
puts "Your generated password is: {password}"

总结

本文介绍了如何使用Ruby语言编写一个简单的密码生成器CLI,并逐步提升了其功能。通过添加复杂度等级、随机性增强和用户界面优化,我们使密码生成器更加灵活和实用。在实际应用中,可以根据需求进一步扩展和优化密码生成器,例如添加密码强度检测、生成多个密码等功能。