1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-26 23:33:03 +00:00

new password generator with special chars

This commit is contained in:
nishiki 2016-07-10 11:00:53 +02:00
parent 7cde7de1ed
commit 712ac9d998
4 changed files with 40 additions and 8 deletions

22
bin/mpw
View file

@ -48,6 +48,7 @@ I18n.locale = lang.to_sym
# Options
# --------------------------------------------------------- #
options_password = {}
options = {}
options[:force] = false
options[:sync] = true
@ -97,8 +98,7 @@ OptionParser.new do |opts|
end
opts.on('-G', '--generate-password [LENGTH]', I18n.t('option.generate_password')) do |length|
puts MPW::MPW::password(length)
exit 0
options_password[:length] = length
end
opts.on('-h', '--help', I18n.t('option.help')) do
@ -118,6 +118,10 @@ OptionParser.new do |opts|
options[:key] = key
end
opts.on('-n', '--numeric', I18n.t('option.numeric')) do
options_password[:numeric] = true
end
opts.on('-N', '--no-sync', I18n.t('option.no_sync')) do
options[:sync] = false
end
@ -141,12 +145,26 @@ OptionParser.new do |opts|
opts.on('-W', '--setup-wallet', I18n.t('option.setup_wallet')) do
options[:setup_wallet] = true
end
opts.on('-x', '--special-chars', I18n.t('option.special_chars')) do
options_password[:special] = true
end
opts.on('-y', '--alpha', I18n.t('option.alpha')) do
options_password[:alpha] = true
end
end.parse!
# --------------------------------------------------------- #
# Main
# --------------------------------------------------------- #
# Generate password
if not options_password.empty?
puts MPW::MPW::password(options_password)
exit 0
end
begin
config = MPW::Config.new(options[:config])
cli = MPW::Cli.new(config, options[:clipboard], options[:sync])

View file

@ -37,6 +37,7 @@ en:
option:
add: "Add an item or key"
alpha: "Use letter to generate a password"
config: "Specify the configuration file to use"
clipboard: "Disable the clipboard feature"
export: "Export a wallet in an yaml file"
@ -49,8 +50,10 @@ en:
import: "Import item since a yaml file"
key: "Specify the key name, to use with the options [--add | --delete | --update]"
no_sync: "Disable synchronization with the server"
numeric: "Use number to generate a password"
setup: "Create a new configuration file"
setup_wallet: "Create a new configuration file for a wallet"
special_chars: "Use special char to generate a password"
show: "Search and show the items"
show_all: "List all items"
remove: "Delete an item"
@ -136,6 +139,7 @@ en:
group: "Group"
login: "Login"
name: "Name"
no_group: "Without group"
nothing: "No matches!"
password: "Password"
port: "Port"

View file

@ -37,6 +37,7 @@ fr:
option:
add: "Ajoute un élément ou une clé"
alpha: "Utilise des lettres dans la génération d'un mot de passe"
config: "Spécifie le fichier de configuration à utiliser"
clipboard: "Désactive la fonction presse papier"
export: "Exporte un portefeuille dans un fichier yaml"
@ -49,8 +50,10 @@ fr:
import: "Importe des éléments depuis un fichier yaml"
key: "Spécifie le nom d'une clé, à utiliser avec les options [--add | --delete | --update]"
no_sync: "Désactive la synchronisation avec le serveur"
numeric: "Utilise des chiffre dans la génération d'un mot de passe"
setup: "Création d'un nouveau fichier de configuration"
setup_wallet: "Création d'un nouveau fichier de configuration pour un portefeuille"
special_chars: "Utilise des charactères speciaux dans la génération d'un mot de passe"
show: "Recherche et affiche les éléments"
show_all: "Liste tous les éléments"
remove: "Supprime un élément"
@ -136,6 +139,7 @@ fr:
group: "Groupe"
login: "Identifiant"
name: "Nom"
no_group: "Sans groupe"
nothing: "Aucun résultat!"
password: "Mot de passe"
port: "Port"

View file

@ -414,21 +414,27 @@ class MPW
end
# Generate a random password
# @args: length -> the length password
# @args: options -> :length, :special, :alpha, :numeric
# @rtrn: a random string
def self.password(length=8)
if length.to_i <= 0
def self.password(options={})
if not options.include?(:length) or options[:length].to_i <= 0
length = 8
else
length = length.to_i
length = options[:length].to_i
end
chars = []
chars += [*('!'..'?')] - [*('0'..'9')] if options.include?(:special)
chars += [*('A'..'Z'),*('a'..'z')] if options.include?(:alpha)
chars += [*('0'..'9')] if options.include?(:numeric)
chars = [*('A'..'Z'),*('a'..'z'),*('0'..'9')] if chars.empty?
result = ''
while length > 62 do
result << ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(62).join
result << chars.sample(62).join
length -= 62
end
result << ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(length).join
result << chars.sample(length).join
return result
end