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:
parent
7cde7de1ed
commit
712ac9d998
4 changed files with 40 additions and 8 deletions
22
bin/mpw
22
bin/mpw
|
@ -48,6 +48,7 @@ I18n.locale = lang.to_sym
|
||||||
# Options
|
# Options
|
||||||
# --------------------------------------------------------- #
|
# --------------------------------------------------------- #
|
||||||
|
|
||||||
|
options_password = {}
|
||||||
options = {}
|
options = {}
|
||||||
options[:force] = false
|
options[:force] = false
|
||||||
options[:sync] = true
|
options[:sync] = true
|
||||||
|
@ -97,8 +98,7 @@ OptionParser.new do |opts|
|
||||||
end
|
end
|
||||||
|
|
||||||
opts.on('-G', '--generate-password [LENGTH]', I18n.t('option.generate_password')) do |length|
|
opts.on('-G', '--generate-password [LENGTH]', I18n.t('option.generate_password')) do |length|
|
||||||
puts MPW::MPW::password(length)
|
options_password[:length] = length
|
||||||
exit 0
|
|
||||||
end
|
end
|
||||||
|
|
||||||
opts.on('-h', '--help', I18n.t('option.help')) do
|
opts.on('-h', '--help', I18n.t('option.help')) do
|
||||||
|
@ -118,6 +118,10 @@ OptionParser.new do |opts|
|
||||||
options[:key] = key
|
options[:key] = key
|
||||||
end
|
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
|
opts.on('-N', '--no-sync', I18n.t('option.no_sync')) do
|
||||||
options[:sync] = false
|
options[:sync] = false
|
||||||
end
|
end
|
||||||
|
@ -141,12 +145,26 @@ OptionParser.new do |opts|
|
||||||
opts.on('-W', '--setup-wallet', I18n.t('option.setup_wallet')) do
|
opts.on('-W', '--setup-wallet', I18n.t('option.setup_wallet')) do
|
||||||
options[:setup_wallet] = true
|
options[:setup_wallet] = true
|
||||||
end
|
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!
|
end.parse!
|
||||||
|
|
||||||
# --------------------------------------------------------- #
|
# --------------------------------------------------------- #
|
||||||
# Main
|
# Main
|
||||||
# --------------------------------------------------------- #
|
# --------------------------------------------------------- #
|
||||||
|
|
||||||
|
# Generate password
|
||||||
|
if not options_password.empty?
|
||||||
|
puts MPW::MPW::password(options_password)
|
||||||
|
exit 0
|
||||||
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
config = MPW::Config.new(options[:config])
|
config = MPW::Config.new(options[:config])
|
||||||
cli = MPW::Cli.new(config, options[:clipboard], options[:sync])
|
cli = MPW::Cli.new(config, options[:clipboard], options[:sync])
|
||||||
|
|
|
@ -37,6 +37,7 @@ en:
|
||||||
|
|
||||||
option:
|
option:
|
||||||
add: "Add an item or key"
|
add: "Add an item or key"
|
||||||
|
alpha: "Use letter to generate a password"
|
||||||
config: "Specify the configuration file to use"
|
config: "Specify the configuration file to use"
|
||||||
clipboard: "Disable the clipboard feature"
|
clipboard: "Disable the clipboard feature"
|
||||||
export: "Export a wallet in an yaml file"
|
export: "Export a wallet in an yaml file"
|
||||||
|
@ -49,8 +50,10 @@ en:
|
||||||
import: "Import item since a yaml file"
|
import: "Import item since a yaml file"
|
||||||
key: "Specify the key name, to use with the options [--add | --delete | --update]"
|
key: "Specify the key name, to use with the options [--add | --delete | --update]"
|
||||||
no_sync: "Disable synchronization with the server"
|
no_sync: "Disable synchronization with the server"
|
||||||
|
numeric: "Use number to generate a password"
|
||||||
setup: "Create a new configuration file"
|
setup: "Create a new configuration file"
|
||||||
setup_wallet: "Create a new configuration file for a wallet"
|
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: "Search and show the items"
|
||||||
show_all: "List all items"
|
show_all: "List all items"
|
||||||
remove: "Delete an item"
|
remove: "Delete an item"
|
||||||
|
@ -136,6 +139,7 @@ en:
|
||||||
group: "Group"
|
group: "Group"
|
||||||
login: "Login"
|
login: "Login"
|
||||||
name: "Name"
|
name: "Name"
|
||||||
|
no_group: "Without group"
|
||||||
nothing: "No matches!"
|
nothing: "No matches!"
|
||||||
password: "Password"
|
password: "Password"
|
||||||
port: "Port"
|
port: "Port"
|
||||||
|
|
|
@ -37,6 +37,7 @@ fr:
|
||||||
|
|
||||||
option:
|
option:
|
||||||
add: "Ajoute un élément ou une clé"
|
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"
|
config: "Spécifie le fichier de configuration à utiliser"
|
||||||
clipboard: "Désactive la fonction presse papier"
|
clipboard: "Désactive la fonction presse papier"
|
||||||
export: "Exporte un portefeuille dans un fichier yaml"
|
export: "Exporte un portefeuille dans un fichier yaml"
|
||||||
|
@ -49,8 +50,10 @@ fr:
|
||||||
import: "Importe des éléments depuis un fichier yaml"
|
import: "Importe des éléments depuis un fichier yaml"
|
||||||
key: "Spécifie le nom d'une clé, à utiliser avec les options [--add | --delete | --update]"
|
key: "Spécifie le nom d'une clé, à utiliser avec les options [--add | --delete | --update]"
|
||||||
no_sync: "Désactive la synchronisation avec le serveur"
|
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: "Création d'un nouveau fichier de configuration"
|
||||||
setup_wallet: "Création d'un nouveau fichier de configuration pour un portefeuille"
|
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: "Recherche et affiche les éléments"
|
||||||
show_all: "Liste tous les éléments"
|
show_all: "Liste tous les éléments"
|
||||||
remove: "Supprime un élément"
|
remove: "Supprime un élément"
|
||||||
|
@ -136,6 +139,7 @@ fr:
|
||||||
group: "Groupe"
|
group: "Groupe"
|
||||||
login: "Identifiant"
|
login: "Identifiant"
|
||||||
name: "Nom"
|
name: "Nom"
|
||||||
|
no_group: "Sans groupe"
|
||||||
nothing: "Aucun résultat!"
|
nothing: "Aucun résultat!"
|
||||||
password: "Mot de passe"
|
password: "Mot de passe"
|
||||||
port: "Port"
|
port: "Port"
|
||||||
|
|
|
@ -414,21 +414,27 @@ class MPW
|
||||||
end
|
end
|
||||||
|
|
||||||
# Generate a random password
|
# Generate a random password
|
||||||
# @args: length -> the length password
|
# @args: options -> :length, :special, :alpha, :numeric
|
||||||
# @rtrn: a random string
|
# @rtrn: a random string
|
||||||
def self.password(length=8)
|
def self.password(options={})
|
||||||
if length.to_i <= 0
|
if not options.include?(:length) or options[:length].to_i <= 0
|
||||||
length = 8
|
length = 8
|
||||||
else
|
else
|
||||||
length = length.to_i
|
length = options[:length].to_i
|
||||||
end
|
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 = ''
|
result = ''
|
||||||
while length > 62 do
|
while length > 62 do
|
||||||
result << ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(62).join
|
result << chars.sample(62).join
|
||||||
length -= 62
|
length -= 62
|
||||||
end
|
end
|
||||||
result << ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(length).join
|
result << chars.sample(length).join
|
||||||
|
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue