scripts: add usign secret to public key converter
authorJo-Philipp Wich <jo@mein.io>
Fri, 28 Jun 2019 11:09:56 +0000 (13:09 +0200)
committerJo-Philipp Wich <jo@mein.io>
Sun, 30 Jun 2019 19:57:19 +0000 (21:57 +0200)
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
scripts/sec2pubkey.pl [new file with mode: 0755]

diff --git a/scripts/sec2pubkey.pl b/scripts/sec2pubkey.pl
new file mode 100755 (executable)
index 0000000..60fff38
--- /dev/null
@@ -0,0 +1,25 @@
+#!/usr/bin/env perl
+
+use strict;
+use MIME::Base64;
+
+my @lines = (-t STDIN) ? () : <>;
+
+if (@lines == 0) {
+       die "Usage: $0 < key.sec > key.pub\n";
+}
+
+my $seckey = decode_base64(pop @lines);
+my $comment = shift(@lines) || "untrusted comment: secret key";
+
+chomp($comment);
+
+$comment =~ s/\bsecret key$/public key/;
+
+if (length($seckey) != 104) {
+       die "Unexpected secret key length\n";
+}
+
+my $pubkey = encode_base64(substr($seckey, 0, 2) . substr($seckey, 32, 8) . substr($seckey, 72), "");
+
+printf "%s\n%s\n", $comment, $pubkey;