Add bin/setpasswd to set user password

This commit is contained in:
Josh Holtrop 2026-03-28 19:47:20 -04:00
parent f31e97b014
commit 518ee7c366
2 changed files with 30 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/user.txt

29
bin/setpasswd Executable file
View 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"