本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
727B

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package secret
  5. import (
  6. "crypto/rand"
  7. "encoding/base64"
  8. )
  9. // New creats a new secret
  10. func New() (string, error) {
  11. return NewWithLength(32)
  12. }
  13. // NewWithLength creates a new secret for a given length
  14. func NewWithLength(length int64) (string, error) {
  15. return randomString(length)
  16. }
  17. func randomBytes(len int64) ([]byte, error) {
  18. b := make([]byte, len)
  19. if _, err := rand.Read(b); err != nil {
  20. return nil, err
  21. }
  22. return b, nil
  23. }
  24. func randomString(len int64) (string, error) {
  25. b, err := randomBytes(len)
  26. return base64.URLEncoding.EncodeToString(b), err
  27. }
上海开阖软件有限公司 沪ICP备12045867号-1