Skip to content

Instantly share code, notes, and snippets.

@yoggy
Last active February 3, 2018 08:41
Show Gist options
  • Select an option

  • Save yoggy/3bed8dd7c8d43531a03f to your computer and use it in GitHub Desktop.

Select an option

Save yoggy/3bed8dd7c8d43531a03f to your computer and use it in GitHub Desktop.
passgen.rb - pseudo password generator
#!/usr/bin/ruby
#
# passgen.rb - pseudo password generator
#
# How to use:
# $ echo login.password.google.com | ./passgen.rb
#
# License:
# Copyright (c) 2018 yoggy <[email protected]>
# Released under the MIT license
# http://opensource.org/licenses/mit-license.php;
#
require 'openssl'
require 'base64'
require 'optparse'
secret_key = "secret_key"
len = 16
encode_char = "0123456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%-=+*@"
OptionParser.new do |opt|
opt.on('-l', '--length=VALUE', 'hash string length') {|v| len = v.to_i}
opt.on('-s', '--secret_key=VALUE', 'secret key') {|v| secret_key = v}
opt.on('-e', '--encode_char=VALUE', 'encode char') {|v| encode_char=v}
opt.parse!(ARGV)
end
s = $stdin.read()
s.chomp()
sha256 = OpenSSL::HMAC.digest('sha256', secret_key, s)
total = 0
sha256.bytes do |b|
total = total * 256
total += b
end
encode_str = ""
((Math.log(total, encode_char.size)+1).floor).times do
i = total % encode_char.size
encode_str << encode_char[i]
total = total / encode_char.size
end
if encode_str.size > len
encode_str = encode_str[0, len]
end
puts encode_str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment