func main() { flag.Parse() b, err := ioutil.ReadAll(os.Stdin) if err != nil { fmt.Fprintf(os.Stderr, "%s\n", err) os.Exit(1) } alpha := *fAlphabet enc, ok := encodings[alpha] if !ok { if len(alpha) != 32 { fmt.Fprintf(os.Stderr, "unknown alphabet: %s\n", alpha) os.Exit(2) } enc = base32.NewEncoding(alpha) } s := enc.EncodeToString(b) if *fLowerCase { s = strings.ToLower(s) } if *fTrimPadding { s = strings.TrimRight(s, "=") } g := *fGroup if g > 0 { rs := "" for i, r := range s { if i > 0 && i%g == 0 { rs += *fGroupSep } rs += string(r) } s = rs } fmt.Println(s) }
} } func NewLocAppError(where string, id string, params map[string]interface{}, details string) *AppError { ap := &AppError{} ap.Id = id ap.params = params ap.Message = id ap.Where = where ap.DetailedError = details ap.StatusCode = 500 ap.IsOAuth = false return ap } var encoding = base32.NewEncoding("ybndrfg8ejkmcpqxot1uwisza345h769") // NewId is a globally unique identifier. It is a [A-Z0-9] string 26 // characters long. It is a UUID version 4 Guid that is zbased32 encoded // with the padding stripped off. func NewId() string { var b bytes.Buffer encoder := base32.NewEncoder(encoding, &b) encoder.Write(uuid.NewRandom()) encoder.Close() b.Truncate(26) // removes the '==' padding return b.String() } func NewRandomString(length int) string { var b bytes.Buffer
) // RFC 4648 without padding const base32EncodeHexLower = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" const padding_rune = '=' const padding_1 = "=" const padding_2 = "==" const padding_3 = "===" const padding_4 = "====" const padding_5 = "=====" const padding_6 = "======" const padding_7 = "=======" var base32EncodeHexLowerInstance = base32.NewEncoding(base32EncodeHexLower) func Base32Decode(str string) ([]byte, error) { length := len(str) remainder := length % 8 if remainder != 0 { missing := 8 - remainder newString := make([]byte, length+missing) copy(newString, str) copy(newString[length:], padding(missing)) return base32EncodeHexLowerInstance.DecodeString(string(newString)) } else { return base32EncodeHexLowerInstance.DecodeString(str) } }
"os" "strings" ) var ( fLowerCase = flag.Bool("l", false, "lower case") fTrimPadding = flag.Bool("p", false, "remove padding") fAlphabet = flag.String("a", "", `alphabet ("" = standard, "hex", "zooko", or alphabet characters)`) fGroup = flag.Int("g", 0, "split into groups of N characters") fGroupSep = flag.String("gs", " ", "group separator for -g option") ) var encodings = map[string]*base32.Encoding{ "": base32.StdEncoding, "hex": base32.HexEncoding, "zooko": base32.NewEncoding("YBNDRFG8EJKMCPQXOT1UWISZA345H769"), } func main() { flag.Parse() b, err := ioutil.ReadAll(os.Stdin) if err != nil { fmt.Fprintf(os.Stderr, "%s\n", err) os.Exit(1) } alpha := *fAlphabet enc, ok := encodings[alpha] if !ok { if len(alpha) != 32 { fmt.Fprintf(os.Stderr, "unknown alphabet: %s\n", alpha) os.Exit(2)
// Copyright 2016 Attic Labs, Inc. All rights reserved. // Licensed under the Apache License, version 2.0: // http://www.apache.org/licenses/LICENSE-2.0 package hash import "encoding/base32" var encoding = base32.NewEncoding("0123456789abcdefghijklmnopqrstuv") func encode(data []byte) string { return encoding.EncodeToString(data) } func decode(s string) []byte { slice, _ := encoding.DecodeString(s) return slice }
// API version of the oidc resources. For example "oidc.coreos.com". This is // currently not configurable, but could be in the future. apiVersion string // This is called once the client's Close method is called to signal goroutines, // such as the one creating third party resources, to stop. cancel context.CancelFunc } // idToName maps an arbitrary ID, such as an email or client ID to a Kubernetes object name. func (c *client) idToName(s string) string { return idToName(s, c.hash) } // Kubernetes names must match the regexp '[a-z0-9]([-a-z0-9]*[a-z0-9])?'. var encoding = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567") func idToName(s string, h func() hash.Hash) string { return strings.TrimRight(encoding.EncodeToString(h().Sum([]byte(s))), "=") } func (c *client) urlFor(apiVersion, namespace, resource, name string) string { basePath := "apis/" if apiVersion == "v1" { basePath = "api/" } var p string if namespace != "" { p = path.Join(basePath, apiVersion, "namespaces", namespace, resource, name) } else {
pod.Name = simpleSubdomainSafeHash(filename) if len(pod.UID) == 0 { pod.UID = simpleSubdomainSafeHash(filename) } if len(pod.Namespace) == 0 { pod.Namespace = api.NamespaceDefault } if glog.V(4) { glog.Infof("Got pod from file %q: %#v", filename, pod) } else { glog.V(1).Infof("Got pod from file %q: %s.%s (%s)", filename, pod.Namespace, pod.Name, pod.UID) } return pod, nil } var simpleSubdomainSafeEncoding = base32.NewEncoding("0123456789abcdefghijklmnopqrstuv") var unsafeDNSLabelReplacement = regexp.MustCompile("[^a-z0-9]+") // simpleSubdomainSafeHash generates a pod name for the given path that is // suitable as a subdomain label. func simpleSubdomainSafeHash(path string) string { name := strings.ToLower(filepath.Base(path)) name = unsafeDNSLabelReplacement.ReplaceAllString(name, "") hasher := sha1.New() hasher.Write([]byte(path)) sha := simpleSubdomainSafeEncoding.EncodeToString(hasher.Sum(nil)) return fmt.Sprintf("%.15s%.30s", name, sha) }
type WriteCloser struct { io.Writer io.Closer } func constructMAC(message, key []byte) []byte { mac := hmac.New(sha256.New, key) mac.Write(message) return mac.Sum(nil) } func checkMAC(message, messageMAC, key []byte) bool { return hmac.Equal(messageMAC, constructMAC(message, key)) } var base32Encoder = base32.NewEncoding("abcdefghjkmnopqrstuvwxyz23456789") func generateRandomBytes(nbytes int) ([]byte, error) { uuid := make([]byte, nbytes) n, err := rand.Read(uuid) if n != len(uuid) || err != nil { return []byte{}, err } return uuid, nil } func generateRandomBase32String(nbytes, outlen int) (string, error) { uuid, err := generateRandomBytes(nbytes) if err != nil { return "", err
package encoding import ( "encoding/base32" "encoding/base64" ) const i2pB64alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-~" // Base64I2P is base64 encoding that is used for destination keys in i2p var Base64I2P = base64.NewEncoding(i2pB64alphabet) type tBase32I2PEncoding struct{} const base32I2PEncodingDic = "abcdefghijklmnopqrstuvwxyz234567" var base32Encoding = base32.NewEncoding(base32I2PEncodingDic) func (b tBase32I2PEncoding) EncodeToString(src []byte) (s string) { s = base32Encoding.EncodeToString(src) for s[len(s)-1] == '=' { s = s[:len(s)-1] } return s } // Base32I2P is base32 encoding that is used for *.b32.i2p names // It is sha256(destination key) in lowercase base32 with no padding (= characters) var Base32I2P = tBase32I2PEncoding{}
// // base32 encoding using I2P's alphabet // package base32 import ( b32 "encoding/base32" ) var I2PEncoding *b32.Encoding = b32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567") // // Return a go string of the I2P base32 // encoding of the provided byte slice // func EncodeToString(data []byte) string { return I2PEncoding.EncodeToString(data) }
func ExampleNewEncoding1() { enc := base32.NewEncoding(encodeTest) src := "this is a test string." dst := enc.EncodeToString([]byte(src)) // 最后不足8字节的会用"="补全 fmt.Println(dst) fmt.Println(len(dst)%8 == 0) // Output: // ------------------------------------==== // true }