Documentation ¶
Overview ¶
Package unchained provides password hashers that are compatible with Django.
These hashers can be also used to perform validation against legacy and shared Django databases.
Django provides a flexible password storage system and uses PBKDF2 by default.
The password format/representation is the same as the one used in Django:
<algorithm>$<iterations>$<salt>$<hash>
This library supports Argon2, BCrypt, PBKDF2, MD5 and SHA1 algorithms.
Index ¶
- Constants
- Variables
- func CheckPassword(password, encoded string) (bool, error)
- func GetRandomString(length int) string
- func IdentifyHasher(encoded string) string
- func IsHasherImplemented(hasher string) bool
- func IsPasswordUsable(encoded string) bool
- func IsValidHasher(hasher string) bool
- func IsWeakHasher(hasher string) bool
- func MakePassword(password, salt, hasher string) (string, error)
Examples ¶
Constants ¶
const ( Argon2Hasher = "argon2" BCryptHasher = "bcrypt" BCryptSHA256Hasher = "bcrypt_sha256" CryptHasher = "crypt" MD5Hasher = "md5" PBKDF2SHA1Hasher = "pbkdf2_sha1" PBKDF2SHA256Hasher = "pbkdf2_sha256" SHA1Hasher = "sha1" UnsaltedMD5Hasher = "unsalted_md5" UnsaltedSHA1Hasher = "unsalted_sha1" )
Django hasher identifiers.
const ( // The prefix used in unusable passwords. UnusablePasswordPrefix = "!" // The length of unusable passwords after the prefix. UnusablePasswordSuffixLength = 40 // The default hasher used in Django. DefaultHasher = PBKDF2SHA256Hasher // The default salt size used in Django. DefaultSaltSize = 12 )
Variables ¶
var ( // ErrInvalidHasher is returned if the hasher is invalid or unknown. ErrInvalidHasher = errors.New("unchained: invalid hasher") // ErrHasherNotImplemented is returned if the hasher is not implemented. ErrHasherNotImplemented = errors.New("unchained: hasher not implemented") )
Functions ¶
func CheckPassword ¶
CheckPassword validates if the raw password matches the encoded digest.
This is a shortcut that discovers the hasher used in the encoded digest to perform the correct validation.
Example ¶
package main import ( "fmt" "github.com/alexandrevicenzi/unchained" ) func main() { valid, err := unchained.CheckPassword("admin", "pbkdf2_sha256$24000$JMO9TJawIXB1$5iz40fwwc+QW6lZY+TuNciua3YVMV3GXdgkhXrcvWag=") if valid { fmt.Println("Password is valid.") } else { if err == nil { fmt.Println("Password is valid.") } else { fmt.Printf("Error decoding password: %s\n", err) } } }
Output:
func GetRandomString ¶ added in v1.1.0
GetRandomString returns a securely generated random string.
func IdentifyHasher ¶ added in v1.1.0
IdentifyHasher returns the hasher used in the encoded password.
func IsHasherImplemented ¶ added in v1.1.0
IsHasherImplemented returns true if the hasher is implemented in this library, or false otherwise.
func IsPasswordUsable ¶
IsPasswordUsable returns true if encoded password is usable, or false otherwise.
func IsValidHasher ¶ added in v1.1.0
IsValidHasher returns true if the hasher is supported by Django, or false otherwise.
func IsWeakHasher ¶ added in v1.1.0
IsWeakHasher returns true if the hasher is not recommend by Django, or false otherwise.
func MakePassword ¶ added in v1.1.0
MakePassword turns a plain-text password into a hash.
If password is empty then return a concatenation of UnusablePasswordPrefix and a random string. If salt is empty then a randon string is generated. BCrypt algorithm ignores salt parameter. If hasher is "default", encode using default hasher.
Example ¶
package main import ( "fmt" "github.com/alexandrevicenzi/unchained" ) func main() { hash, err := unchained.MakePassword("my-password", unchained.GetRandomString(12), "default") if err == nil { fmt.Println(hash) } else { fmt.Printf("Error encoding password: %s\n", err) } }
Output:
Types ¶
This section is empty.
Directories ¶
Path | Synopsis |
---|---|
Package argon2 implements a Django compatible Argon2 algorithm.
|
Package argon2 implements a Django compatible Argon2 algorithm. |
Package bcrypt implements a Django compatible bcrypt algorithm.
|
Package bcrypt implements a Django compatible bcrypt algorithm. |
Package md5 implements a Django compatible MD5 algorithm.
|
Package md5 implements a Django compatible MD5 algorithm. |
Package pbkdf2 implements a Django compatible PBKDF2 algorithm.
|
Package pbkdf2 implements a Django compatible PBKDF2 algorithm. |
Package sha1 implements a Django compatible SHA1 algorithm.
|
Package sha1 implements a Django compatible SHA1 algorithm. |