2020-01-22 05:45:04 +07:00
package commands
import (
"fmt"
2020-04-05 19:37:21 +07:00
"github.com/spf13/cobra"
2020-01-22 05:45:04 +07:00
"github.com/authelia/authelia/internal/authentication"
2020-03-06 08:38:02 +07:00
"github.com/authelia/authelia/internal/configuration/schema"
2020-01-22 05:45:04 +07:00
)
2020-03-06 08:38:02 +07:00
func init ( ) {
2020-04-11 10:54:18 +07:00
HashPasswordCmd . Flags ( ) . BoolP ( "sha512" , "z" , false , fmt . Sprintf ( "use sha512 as the algorithm (changes iterations to %d, change with -i)" , schema . DefaultPasswordSHA512Configuration . Iterations ) )
HashPasswordCmd . Flags ( ) . IntP ( "iterations" , "i" , schema . DefaultPasswordConfiguration . Iterations , "set the number of hashing iterations" )
2020-03-06 08:38:02 +07:00
HashPasswordCmd . Flags ( ) . StringP ( "salt" , "s" , "" , "set the salt string" )
2020-04-11 10:54:18 +07:00
HashPasswordCmd . Flags ( ) . IntP ( "memory" , "m" , schema . DefaultPasswordConfiguration . Memory , "[argon2id] set the amount of memory param (in MB)" )
HashPasswordCmd . Flags ( ) . IntP ( "parallelism" , "p" , schema . DefaultPasswordConfiguration . Parallelism , "[argon2id] set the parallelism param" )
HashPasswordCmd . Flags ( ) . IntP ( "key-length" , "k" , schema . DefaultPasswordConfiguration . KeyLength , "[argon2id] set the key length param" )
HashPasswordCmd . Flags ( ) . IntP ( "salt-length" , "l" , schema . DefaultPasswordConfiguration . SaltLength , "set the auto-generated salt length" )
2020-03-06 08:38:02 +07:00
}
2020-04-21 04:03:38 +07:00
// HashPasswordCmd password hashing command.
2020-01-22 05:45:04 +07:00
var HashPasswordCmd = & cobra . Command {
Use : "hash-password [password]" ,
2020-03-06 08:38:02 +07:00
Short : "Hash a password to be used in file-based users database. Default algorithm is argon2id." ,
2020-01-22 05:45:04 +07:00
Run : func ( cobraCmd * cobra . Command , args [ ] string ) {
2020-03-06 08:38:02 +07:00
sha512 , _ := cobraCmd . Flags ( ) . GetBool ( "sha512" )
iterations , _ := cobraCmd . Flags ( ) . GetInt ( "iterations" )
salt , _ := cobraCmd . Flags ( ) . GetString ( "salt" )
keyLength , _ := cobraCmd . Flags ( ) . GetInt ( "key-length" )
saltLength , _ := cobraCmd . Flags ( ) . GetInt ( "salt-length" )
memory , _ := cobraCmd . Flags ( ) . GetInt ( "memory" )
parallelism , _ := cobraCmd . Flags ( ) . GetInt ( "parallelism" )
var err error
var hash string
2020-05-03 11:06:09 +07:00
var algorithm authentication . CryptAlgo
2020-03-06 08:38:02 +07:00
if sha512 {
2020-04-11 10:54:18 +07:00
if iterations == schema . DefaultPasswordConfiguration . Iterations {
iterations = schema . DefaultPasswordSHA512Configuration . Iterations
2020-03-06 08:38:02 +07:00
}
algorithm = authentication . HashingAlgorithmSHA512
} else {
algorithm = authentication . HashingAlgorithmArgon2id
}
hash , err = authentication . HashPassword ( args [ 0 ] , salt , algorithm , iterations , memory * 1024 , parallelism , keyLength , saltLength )
if err != nil {
2020-04-09 08:05:17 +07:00
fmt . Printf ( "Error occurred during hashing: %s" , err )
2020-03-06 08:38:02 +07:00
} else {
2020-04-09 08:05:17 +07:00
fmt . Printf ( "Password hash: %s" , hash )
2020-03-06 08:38:02 +07:00
}
2020-01-22 05:45:04 +07:00
} ,
Args : cobra . MinimumNArgs ( 1 ) ,
}