2020-11-29 14:26:42 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
2020-12-15 15:20:04 +01:00
|
|
|
"fmt"
|
2020-11-29 14:26:42 +01:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/pbkdf2"
|
|
|
|
|
2020-12-19 17:37:48 +01:00
|
|
|
"github.com/jech/galene/group"
|
2020-11-29 14:26:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var iterations int
|
|
|
|
var length int
|
|
|
|
var saltLen int
|
2020-12-15 15:20:04 +01:00
|
|
|
var username string
|
|
|
|
flag.StringVar(&username, "user", "",
|
|
|
|
"generate entry for given `username`")
|
|
|
|
flag.IntVar(&iterations, "iterations", 4096, "`number` of iterations")
|
|
|
|
flag.IntVar(&length, "key", 32, "key `length`")
|
|
|
|
flag.IntVar(&saltLen, "salt", 8, "salt `length`")
|
2020-11-29 14:26:42 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if len(flag.Args()) == 0 {
|
2020-12-15 15:20:04 +01:00
|
|
|
fmt.Fprintf(
|
|
|
|
flag.CommandLine.Output(),
|
|
|
|
"Usage: %s [option...] password...\n",
|
|
|
|
os.Args[0])
|
|
|
|
flag.PrintDefaults()
|
2020-11-29 14:26:42 +01:00
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
salt := make([]byte, saltLen)
|
|
|
|
|
|
|
|
for _, pw := range flag.Args() {
|
|
|
|
_, err := rand.Read(salt)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Salt: %v", err)
|
|
|
|
}
|
|
|
|
key := pbkdf2.Key(
|
|
|
|
[]byte(pw), salt, iterations, length, sha256.New,
|
|
|
|
)
|
|
|
|
|
|
|
|
p := group.Password{
|
|
|
|
Type: "pbkdf2",
|
|
|
|
Hash: "sha-256",
|
|
|
|
Key: hex.EncodeToString(key),
|
|
|
|
Salt: hex.EncodeToString(salt),
|
|
|
|
Iterations: iterations,
|
|
|
|
}
|
|
|
|
e := json.NewEncoder(os.Stdout)
|
2020-12-15 15:20:04 +01:00
|
|
|
if username != "" {
|
2021-10-27 04:15:44 +02:00
|
|
|
creds := group.ClientPattern{
|
2020-12-15 15:20:04 +01:00
|
|
|
Username: username,
|
|
|
|
Password: &p,
|
|
|
|
}
|
|
|
|
err = e.Encode(creds)
|
|
|
|
} else {
|
|
|
|
err = e.Encode(p)
|
|
|
|
}
|
2020-11-29 14:26:42 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Encode: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|