| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package bcrypt |
| 6 |
|
| 7 |
import ( |
| 8 |
"github.com/go-crypt/x/base64" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 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 |
|
| 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 |
} |