選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

bcrypt.go 1.3KB

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright (c) 2018 Shivaram Lingamneni
  2. // released under the MIT license
  3. package passwd
  4. import "golang.org/x/crypto/bcrypt"
  5. import "golang.org/x/crypto/sha3"
  6. const (
  7. MinCost = bcrypt.MinCost
  8. DefaultCost = 12 // ballpark: 250 msec on a modern Intel CPU
  9. )
  10. // implements Dropbox's strategy of applying an initial pass of a "normal"
  11. // (i.e., fast) cryptographically secure hash with 512 bits of output before
  12. // applying bcrypt. This allows the use of, e.g., Diceware/XKCD-style passphrases
  13. // that may be longer than the 80-character bcrypt limit.
  14. // https://blogs.dropbox.com/tech/2016/09/how-dropbox-securely-stores-your-passwords/
  15. // we are only using this for user-generated passwords, as opposed to the server
  16. // and operator passwords that are hashed by `oragono genpasswd` and then
  17. // hard-coded by the server admins into the config file, to avoid breaking
  18. // backwards compatibility (since we can't upgrade the config file on the fly
  19. // the way we can with the database).
  20. func GenerateFromPassword(password []byte, cost int) (result []byte, err error) {
  21. sum := sha3.Sum512(password)
  22. return bcrypt.GenerateFromPassword(sum[:], cost)
  23. }
  24. func CompareHashAndPassword(hashedPassword, password []byte) error {
  25. sum := sha3.Sum512(password)
  26. return bcrypt.CompareHashAndPassword(hashedPassword, sum[:])
  27. }