commit 7133d49

Emilia Smólska  ·  2026-06-10 00:13:00 +0000 UTC
parent 7133d49
meow
1 files changed,  +60, -0
A haslo
A haslo
+60, -0
 1@@ -0,0 +1,60 @@
 2+#!/usr/bin/env perl
 3+
 4+# Public domain
 5+
 6+use strict;
 7+use warnings;
 8+use Term::ReadKey qw(ReadMode ReadLine);
 9+use MIME::Base64 qw(encode_base64);
10+
11+sub usage
12+{
13+	die "usage: haslo put|get|gen name\n";
14+}
15+
16+my $command = $ARGV[0] or usage;
17+my $target = $ARGV[1] or usage;
18+my $home = $ENV{'HOME'} or die "\$HOME not set\n";
19+my $hasloemail = $ENV{'HASLOEMAIL'} or die "\$HASLOEMAIL not set\n";
20+my $haslodir = "$home/.haslo";
21+
22+sub writegpgpassword
23+{
24+	unless(-d $haslodir){
25+		print STDERR "creating directory $haslodir\n";
26+		mkdir $haslodir or die "couldn't create directory $haslodir: $!\n";
27+	}
28+	my $password = shift;
29+	open GPG, '|-', 'gpg', '-qer', $hasloemail, '-o', "$haslodir/$target.gpg" or die "can't open pipe to gpg: $!\n";
30+	print GPG $password;
31+	close GPG or die "gpg failed with exit code $?\n";
32+}
33+
34+if($command eq 'put'){
35+	my($password, $confirm);
36+	do{
37+		ReadMode 'noecho';
38+		print STDERR "enter password for $target: ";
39+		$password = ReadLine 0;
40+		print STDERR "\n";
41+		print STDERR "confirm password: ";
42+		$confirm = ReadLine 0;
43+		print STDERR "\n";
44+		ReadMode 'restore';
45+	}while($password ne $confirm);
46+	writegpgpassword $password;
47+}elsif($command eq 'get'){
48+	open GPG, '-|', 'gpg', '-qd', "$haslodir/$target.gpg" or die "can't open pipe to gpg: $!\n";
49+	my $password = <GPG>;
50+	close GPG or die "gpg failed with exit code $?\n";
51+	print $password;
52+}elsif($command eq 'gen'){
53+	my $buf = '';
54+	open URANDOM, '<', '/dev/urandom' or die "can't open /dev/urandom: $!\n";
55+	read URANDOM, $buf, 128 or die "can't read from /dev/urandom: $!\n";
56+	$buf = encode_base64 $buf;
57+	writegpgpassword $buf;
58+	print $buf;
59+}else{
60+	usage;
61+}