30 lines
726 B
Ruby
Executable File
30 lines
726 B
Ruby
Executable File
#!/usr/bin/env ruby
|
|
|
|
require "digest"
|
|
require "fileutils"
|
|
require "io/console"
|
|
require "securerandom"
|
|
|
|
print "User name: "
|
|
username = $stdin.gets.chomp
|
|
|
|
unless username =~ /^[a-zA-Z0-9_]+$/
|
|
$stderr.puts "Invalid characters in user name"
|
|
exit(1)
|
|
end
|
|
|
|
print "Password: "
|
|
password = $stdin.noecho(&:gets).chomp
|
|
|
|
user_path = File.join(File.dirname(__FILE__), "../user.txt")
|
|
|
|
salt = SecureRandom.random_bytes(8)
|
|
salthex = salt.chars.map {|c| sprintf("%02X", c.ord)}.join
|
|
input = salt + password
|
|
hash = Digest::SHA256.hexdigest(input)
|
|
hashhex = hash.chars.map {|c| sprintf("%02X", c.ord)}.join
|
|
user_file_contents = "#{username}:#{salthex}:#{hashhex}\n"
|
|
|
|
File.binwrite(user_path, user_file_contents)
|
|
puts "User and password set"
|