| 1 |
package base64 |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/base64" |
| 5 |
) |
| 6 |
|
| 7 |
var AdaptedEncoding = base64.NewEncoding(encodeAdapted) |
| 8 |
|
| 9 |
|
| 10 |
var BcryptEncoding = base64.NewEncoding(bcryptB64Alphabet) |
| 11 |
|
| 12 |
|
| 13 |
func EncodeCrypt(src []byte) (dst []byte) { |
| 14 |
if len(src) == 0 { |
| 15 |
return nil |
| 16 |
} |
| 17 |
|
| 18 |
dst = make([]byte, (len(src)*8+5)/6) |
| 19 |
|
| 20 |
idst, isrc := 0, 0 |
| 21 |
|
| 22 |
for isrc < len(src)/3*3 { |
| 23 |
v := uint(src[isrc+2])<<16 | uint(src[isrc+1])<<8 | uint(src[isrc]) |
| 24 |
dst[idst+0] = cryptB64Alphabet[v&0x3f] |
| 25 |
dst[idst+1] = cryptB64Alphabet[v>>6&0x3f] |
| 26 |
dst[idst+2] = cryptB64Alphabet[v>>12&0x3f] |
| 27 |
dst[idst+3] = cryptB64Alphabet[v>>18] |
| 28 |
idst += 4 |
| 29 |
isrc += 3 |
| 30 |
} |
| 31 |
|
| 32 |
remainder := len(src) - isrc |
| 33 |
|
| 34 |
if remainder == 0 { |
| 35 |
return dst |
| 36 |
} |
| 37 |
|
| 38 |
v := uint(src[isrc+0]) |
| 39 |
if remainder == 2 { |
| 40 |
v |= uint(src[isrc+1]) << 8 |
| 41 |
} |
| 42 |
|
| 43 |
dst[idst+0] = cryptB64Alphabet[v&0x3f] |
| 44 |
dst[idst+1] = cryptB64Alphabet[v>>6&0x3f] |
| 45 |
|
| 46 |
if remainder == 2 { |
| 47 |
dst[idst+2] = cryptB64Alphabet[v>>12] |
| 48 |
} |
| 49 |
|
| 50 |
return dst |
| 51 |
} |