From 518ee7c3665ebbe6400ad5735161b3e0976c6f4f Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sat, 28 Mar 2026 19:47:20 -0400 Subject: [PATCH] Add bin/setpasswd to set user password --- .gitignore | 1 + bin/setpasswd | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 .gitignore create mode 100755 bin/setpasswd diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d0217a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/user.txt diff --git a/bin/setpasswd b/bin/setpasswd new file mode 100755 index 0000000..875b14e --- /dev/null +++ b/bin/setpasswd @@ -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"