-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeygen
executable file
·61 lines (50 loc) · 1.91 KB
/
keygen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env elixir
defmodule Keygen do
@defaults %{
keyFilePath: Path.expand("./keyfile"),
keySizeBytes: "32",
fileMode: 0o400
}
def main(["-h"]), do: usage()
def main(["-help"]), do: usage()
def main(["--help"]), do: usage()
def main([]) do
main([@defaults.keyFilePath, @defaults.keySizeBytes])
end
def main([keyFilePath]) do
main([keyFilePath, @defaults.keySizeBytes])
end
def main([keyFilePath, keySizeBytes]) do
case File.stat(keyFilePath) do
{:error, _reason} ->
case Integer.parse(keySizeBytes) do
{keySizeInt, ""} ->
key = :crypto.strong_rand_bytes(keySizeInt)
case File.write(keyFilePath, key, [:raw]) do
:ok ->
File.chmod!(keyFilePath, @defaults.fileMode)
keyHash = :crypto.hash(:sha256, key) |> :binary.encode_hex() |> :string.lowercase()
IO.puts("** Generated #{keySizeInt} bytes of key material with SHA256 checksum #{keyHash}\n")
IO.puts("** Wrote raw key to #{keyFilePath}\n\n")
IO.puts("** Copy/paste the following hexadecimal version into the browser extension:\n")
IO.puts(:binary.encode_hex(key) |> :string.lowercase())
IO.puts("\n** IMPORTANT: The above will *not be shown again*!")
{:error, reason} ->
IO.puts("failed to write #{keyFilePath}: '#{reason}'")
end
_ ->
IO.puts("cannot parse keySizeBytes '#{inspect(keySizeBytes)}'")
end
{:ok, _stat} ->
IO.puts("keyfile already exists, will not overwrite")
end
end
def main(_), do: usage()
defp usage() do
IO.puts("Usage: keygen [keyFilePath] [keySizeBytes]\n\n" <>
"Defaults when not specified:" <>
"\n\tkeyFilePath: #{@defaults.keyFilePath}" <>
"\n\tkeySizeBytes: #{@defaults.keySizeBytes}")
end
end
Keygen.main(System.argv())