base64.go 817 B

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package bcrypt
  5. import "encoding/base64"
  6. const alphabet = "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  7. var bcEncoding = base64.NewEncoding(alphabet)
  8. func base64Encode(src []byte) []byte {
  9. n := bcEncoding.EncodedLen(len(src))
  10. dst := make([]byte, n)
  11. bcEncoding.Encode(dst, src)
  12. for dst[n-1] == '=' {
  13. n--
  14. }
  15. return dst[:n]
  16. }
  17. func base64Decode(src []byte) ([]byte, error) {
  18. numOfEquals := 4 - (len(src) % 4)
  19. for i := 0; i < numOfEquals; i++ {
  20. src = append(src, '=')
  21. }
  22. dst := make([]byte, bcEncoding.DecodedLen(len(src)))
  23. n, err := bcEncoding.Decode(dst, src)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return dst[:n], nil
  28. }