Files
upstream-mirror c60cc7ca75
Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
snapshot of https://github.com/ory/kratos.git (master, 2026-09-15; blobs > 131072B omitted)
2026-09-15 21:19:12 +05:00

84 lines
1.9 KiB
Go

// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package argon2
import (
"fmt"
"time"
"github.com/spf13/cobra"
"github.com/ory/kratos/hash"
"github.com/ory/x/cmdx"
"github.com/ory/x/configx"
"github.com/ory/x/flagx"
)
const (
FlagParallel = "parallel"
)
func newHashCmd() *cobra.Command {
flagConfig := &argon2Config{}
cmd := &cobra.Command{
Use: "hash <password1> [<password2> ...]",
Short: "Hash a list of passwords for benchmarking the hashing parameters",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := configProvider(cmd, flagConfig)
if err != nil {
return err
}
flagConfig.ctx = cmd.Context()
hasher := hash.NewHasherArgon2(conf)
hashes := make([][]byte, len(args))
errs := make(chan error, len(args))
start := time.Now()
for i, pw := range args {
go func(i int, pw string) {
start := time.Now()
h, err := hasher.Generate(cmd.Context(), []byte(pw))
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "password %d: %s\n", i, time.Since(start))
hashes[i] = h
errs <- err
}(i, pw)
if !flagx.MustGetBool(cmd, FlagParallel) {
if err := <-errs; err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not generate hash: %s\n", err.Error())
return cmdx.FailSilently(cmd)
}
}
}
if flagx.MustGetBool(cmd, FlagParallel) {
for i := 0; i < len(args); i++ {
if err := <-errs; err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not generate hash: %s\n", err.Error())
return cmdx.FailSilently(cmd)
}
}
}
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "total: %s\n", time.Since(start))
return nil
},
}
flags := cmd.Flags()
flags.Bool(FlagParallel, false, "Run all hashing operations in parallel.")
registerArgon2ConfigFlags(flags, flagConfig)
configx.RegisterFlags(flags)
return cmd
}