Add bin/setpasswd to set user password
This commit is contained in:
parent
f31e97b014
commit
518ee7c366
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/user.txt
|
||||
29
bin/setpasswd
Executable file
29
bin/setpasswd
Executable file
@ -0,0 +1,29 @@
|
||||
#!/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"
|
||||
Loading…
x
Reference in New Issue
Block a user