Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

scram.go 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2018 by David A. Golden. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. package scram
  7. import (
  8. "crypto/sha1"
  9. "crypto/sha256"
  10. "hash"
  11. )
  12. // HashGeneratorFcn abstracts a factory function that returns a hash.Hash
  13. // value to be used for SCRAM operations. Generally, one would use the
  14. // provided package variables, `scram.SHA1` and `scram.SHA256`, for the most
  15. // common forms of SCRAM.
  16. type HashGeneratorFcn func() hash.Hash
  17. // SHA1 is a function that returns a crypto/sha1 hasher and should be used to
  18. // create Client objects configured for SHA-1 hashing.
  19. var SHA1 HashGeneratorFcn = func() hash.Hash { return sha1.New() }
  20. // SHA256 is a function that returns a crypto/sha256 hasher and should be used
  21. // to create Client objects configured for SHA-256 hashing.
  22. var SHA256 HashGeneratorFcn = func() hash.Hash { return sha256.New() }
  23. // NewClientUnprepped acts like NewClient, except none of the arguments will
  24. // be normalized via SASLprep. This is not generally recommended, but is
  25. // provided for users that may have custom normalization needs.
  26. func (f HashGeneratorFcn) NewClientUnprepped(username, password, authzID string) (*Client, error) {
  27. return newClient(username, password, authzID, f), nil
  28. }
  29. // NewServer constructs a SCRAM server component based on a given hash.Hash
  30. // factory receiver. To be maximally generic, it uses dependency injection to
  31. // handle credential lookup, which is the process of turning a username string
  32. // into a struct with stored credentials for authentication.
  33. func (f HashGeneratorFcn) NewServer(cl CredentialLookup) (*Server, error) {
  34. return newServer(cl, f)
  35. }