Compare commits
No commits in common. "14439a647957cef510f10bb8a57e8f6e3fe3ad1f" and "62f4865324062d904756e3ee226c83df3f2eef0c" have entirely different histories.
14439a6479
...
62f4865324
@ -7,8 +7,3 @@ Strong password highly recomended
|
|||||||
## Password selection
|
## Password selection
|
||||||
To keep entropy at at the same level of $\geq 2^{256}$ bits, as in private key of curve25519 (which is used in age encryption), you should use long password.
|
To keep entropy at at the same level of $\geq 2^{256}$ bits, as in private key of curve25519 (which is used in age encryption), you should use long password.
|
||||||
Exact amount of required characters can be calculated by formula: $\lceil 256 / log_2(Nchars) \rceil$
|
Exact amount of required characters can be calculated by formula: $\lceil 256 / log_2(Nchars) \rceil$
|
||||||
|
|
||||||
|
|
||||||
## TODO
|
|
||||||
[ ] piped/terminal output as raw/verbose
|
|
||||||
[ ] handle broken pipe signal since program will so much depend on pipes
|
|
||||||
|
|||||||
@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@ -22,69 +21,30 @@ type X25519Identity struct {
|
|||||||
secretKey, ourPublicKey []byte
|
secretKey, ourPublicKey []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
const usage = `Usage:
|
|
||||||
age-gen-passphrase [-o OUTPUT] [--raw-input]
|
|
||||||
|
|
||||||
Options:
|
|
||||||
-o, --output OUTPUT Write the result to the file at path OUTPUT.
|
|
||||||
--raw-output Print stripped keys (without additional text or comments)
|
|
||||||
|
|
||||||
Everything is similar to age-keygen`
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.SetFlags(0)
|
|
||||||
flag.Usage = func() { fmt.Fprintf(os.Stderr, "%s\n", usage) }
|
|
||||||
|
|
||||||
var (
|
|
||||||
rawOutput bool
|
|
||||||
outputFile string
|
|
||||||
)
|
|
||||||
|
|
||||||
flag.BoolVar(&rawOutput, "raw-output", false, "Print stripped keys (without additional text or comments)")
|
|
||||||
flag.StringVar(&outputFile, "o", "", "Write the result to the file at path OUTPUT")
|
|
||||||
flag.StringVar(&outputFile, "output", "", "Write the result to the file at path OUTPUT")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
passbytes, err := getPasswordBytes()
|
passbytes, err := getPasswordBytes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorf("Failed to get password, error: %s\n", err)
|
errorf("Failed to get password, error: %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sum := sha256.Sum256(passbytes)
|
sum := sha256.Sum256(passbytes)
|
||||||
|
fmt.Printf("Password hash: %x\n", sum)
|
||||||
|
|
||||||
k, err := newX25519IdentityFromScalar(sum[:])
|
k, err := newX25519IdentityFromScalar(sum[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorf("internal error: %v", err)
|
errorf("internal error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// if user is not seeing private keyfile, which also contains public key,
|
|
||||||
// also duplicate public key it to stderr,
|
|
||||||
// but if user sees public key via stdout, no need for duplication
|
|
||||||
if outputFile != "" {
|
|
||||||
if !rawOutput {
|
|
||||||
fmt.Printf("Public key: %s\n", k.Recipient())
|
fmt.Printf("Public key: %s\n", k.Recipient())
|
||||||
} else {
|
|
||||||
fmt.Printf("%s", k.Recipient())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
output := os.Stdout
|
fmt.Printf("# created: %s\n", time.Now().Format(time.RFC3339))
|
||||||
if outputFile != "" {
|
fmt.Printf("# public key: %s\n", k.Recipient())
|
||||||
output, err = os.Create(outputFile)
|
fmt.Printf("%s\n", k)
|
||||||
if err != nil {
|
|
||||||
errorf("failed to create output file, error: %s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = writeSecretKey(output, k, !rawOutput)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to write secret key to file, error: %s\n", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPasswordBytes() ([]byte, error) {
|
func getPasswordBytes() ([]byte, error) {
|
||||||
if term.IsTerminal(int(os.Stdin.Fd())) {
|
if term.IsTerminal(int(os.Stdin.Fd())) {
|
||||||
fmt.Fprintf(os.Stderr, "Enter password: ")
|
fmt.Print("Enter password: ")
|
||||||
passbytes, err := term.ReadPassword(int(os.Stdin.Fd()))
|
passbytes, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
return passbytes, err
|
return passbytes, err
|
||||||
@ -93,28 +53,6 @@ func getPasswordBytes() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeSecretKey(f *os.File, key *age.X25519Identity, verbose bool) error {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
if verbose {
|
|
||||||
_, err = fmt.Fprintf(f, "# created: %s\n", time.Now().Format(time.RFC3339))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = fmt.Fprintf(f, "# public key: %s\n", key.Recipient())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = fmt.Fprintf(f, "%s\n", key)
|
|
||||||
} else {
|
|
||||||
_, err = fmt.Fprintf(f, "%s", key)
|
|
||||||
}
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// almost a copy of private function in age/x25519.go
|
// almost a copy of private function in age/x25519.go
|
||||||
func newX25519IdentityFromScalar(secretKey []byte) (*age.X25519Identity, error) {
|
func newX25519IdentityFromScalar(secretKey []byte) (*age.X25519Identity, error) {
|
||||||
if len(secretKey) != curve25519.ScalarSize {
|
if len(secretKey) != curve25519.ScalarSize {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user