Add bin/setpasswd to set user password

This commit is contained in:
josh committed 2026-03-28 19:47:20 -04:00
1 parent f31e97b014
commit 518ee7c366
2 files changed
+30

No files matched your search

Executable
+29
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"