Compare commits

..

6 Commits

Author SHA1 Message Date
akulij
721e68a64e change error print method
used specific for errors function
this one also stops program
2025-02-26 18:33:24 +00:00
akulij
6c40441f70 implement password getter
get password via hidden user input or raw piped stdin
2025-02-26 18:23:30 +00:00
akulij
8e5fcfb1be go mod tidy 2025-02-26 18:21:12 +00:00
akulij
ccd10d8c7f install term package as a dependency 2025-02-26 18:20:19 +00:00
akulij
55f720e419 Add language server gopls to shell config 2025-02-26 18:16:55 +00:00
akulij
e8ea9461ed Create files to develop in nix 2025-02-26 16:36:38 +00:00
6 changed files with 116 additions and 13 deletions

View File

@ -1,10 +1,15 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"io"
"log"
"os"
"time" "time"
"unsafe" "unsafe"
"errors"
"golang.org/x/term"
"crypto/sha256" "crypto/sha256"
"golang.org/x/crypto/curve25519" "golang.org/x/crypto/curve25519"
@ -17,17 +22,17 @@ type X25519Identity struct {
} }
func main() { func main() {
var passphrase string passbytes, err := getPasswordBytes()
if err != nil {
errorf("Failed to get password, error: %s\n", err)
}
fmt.Print("Enter password: ") sum := sha256.Sum256(passbytes)
fmt.Scanln(&passphrase)
sum := sha256.Sum256([]byte(passphrase))
fmt.Printf("Password hash: %x\n", sum) fmt.Printf("Password hash: %x\n", sum)
k, err := newX25519IdentityFromScalar(sum[:]) k, err := newX25519IdentityFromScalar(sum[:])
if err != nil { if err != nil {
fmt.Printf("internal error: %v", err) errorf("internal error: %v", err)
} }
fmt.Printf("Public key: %s\n", k.Recipient()) fmt.Printf("Public key: %s\n", k.Recipient())
@ -37,6 +42,17 @@ func main() {
fmt.Printf("%s\n", k) fmt.Printf("%s\n", k)
} }
func getPasswordBytes() ([]byte, error) {
if term.IsTerminal(int(os.Stdin.Fd())) {
fmt.Print("Enter password: ")
passbytes, err := term.ReadPassword(int(os.Stdin.Fd()))
fmt.Println()
return passbytes, err
} else {
return io.ReadAll(os.Stdin)
}
}
// 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 {
@ -49,3 +65,7 @@ func newX25519IdentityFromScalar(secretKey []byte) (*age.X25519Identity, error)
i.ourPublicKey, _ = curve25519.X25519(i.secretKey, curve25519.Basepoint) i.ourPublicKey, _ = curve25519.X25519(i.secretKey, curve25519.Basepoint)
return (*age.X25519Identity)(unsafe.Pointer(i)), nil return (*age.X25519Identity)(unsafe.Pointer(i)), nil
} }
func errorf(format string, v ...interface{}) {
log.Fatalf("age-gen-passphrase ERROR: "+format, v...)
}

57
flake.lock generated Normal file
View File

@ -0,0 +1,57 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 0,
"narHash": "sha256-7Fu7oazPoYCbDzb9k8D/DdbKrC3aU1zlnc39Y8jy/s8=",
"path": "/nix/store/m4wcdchjxw2fdyzjp8i6irpc613pchkr-source",
"type": "path"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

13
flake.nix Normal file
View File

@ -0,0 +1,13 @@
{
inputs = {
flake-utils.url = "github:numtide/flake-utils";
};
outputs = {self, nixpkgs, flake-utils, ...}@inputs:
flake-utils.lib.eachDefaultSystem
(system:
let pkgs = nixpkgs.legacyPackages.${system}; in
{
devShells.default = import ./shell.nix { inherit pkgs; };
}
);
}

8
go.mod
View File

@ -3,7 +3,9 @@ module github.com/akulij/age-gen-passphrase
go 1.22.2 go 1.22.2
require ( require (
filippo.io/age v1.2.1 // indirect filippo.io/age v1.2.1
golang.org/x/crypto v0.24.0 // indirect golang.org/x/crypto v0.24.0
golang.org/x/sys v0.21.0 // indirect golang.org/x/term v0.29.0
) )
require golang.org/x/sys v0.30.0 // indirect

8
go.sum
View File

@ -1,6 +1,10 @@
c2sp.org/CCTV/age v0.0.0-20240306222714-3ec4d716e805 h1:u2qwJeEvnypw+OCPUHmoZE3IqwfuN5kgDfo5MLzpNM0=
c2sp.org/CCTV/age v0.0.0-20240306222714-3ec4d716e805/go.mod h1:FomMrUJ2Lxt5jCLmZkG3FHa72zUprnhd3v/Z18Snm4w=
filippo.io/age v1.2.1 h1:X0TZjehAZylOIj4DubWYU1vWQxv9bJpo+Uu2/LGhi1o= filippo.io/age v1.2.1 h1:X0TZjehAZylOIj4DubWYU1vWQxv9bJpo+Uu2/LGhi1o=
filippo.io/age v1.2.1/go.mod h1:JL9ew2lTN+Pyft4RiNGguFfOpewKwSHm5ayKD/A4004= filippo.io/age v1.2.1/go.mod h1:JL9ew2lTN+Pyft4RiNGguFfOpewKwSHm5ayKD/A4004=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI= golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM= golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.29.0 h1:L6pJp37ocefwRRtYPKSWOWzOtWSxVajvz2ldH/xi3iU=
golang.org/x/term v0.29.0/go.mod h1:6bl4lRlvVuDgSf3179VpIxBF0o10JUpXWOnI7nErv7s=

7
shell.nix Normal file
View File

@ -0,0 +1,7 @@
{ pkgs ? import <nixpkgs> { } }:
pkgs.mkShell {
buildInputs = [
pkgs.go
pkgs.gopls
];
}