Debian Codegraph @

bcrypt/base64.go

raw symbols SCIP
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
5 package bcrypt
6
7 import (
8 "github.com/go-crypt/x/base64"
9 )
10
11 // Base64Encode is the base64 encoder for bcrypt.
12 func Base64Encode(src []byte) []byte {
13 n := base64.BcryptEncoding.EncodedLen(len(src))
14 dst := make([]byte, n)
15 base64.BcryptEncoding.Encode(dst, src)
16 for dst[n-1] == '=' {
17 n--
18 }
19 return dst[:n]
20 }
21
22 // Base64Decode is the base64 decoder for bcrypt.
23 func Base64Decode(src []byte) ([]byte, error) {
24 numOfEquals := 4 - (len(src) % 4)
25 for i := 0; i < numOfEquals; i++ {
26 src = append(src, '=')
27 }
28
29 dst := make([]byte, base64.BcryptEncoding.DecodedLen(len(src)))
30 n, err := base64.BcryptEncoding.Decode(dst, src)
31 if err != nil {
32 return nil, err
33 }
34 return dst[:n], nil
35 }