當前位置: 首頁>>代碼示例>>Golang>>正文


Golang base32.NewEncoder函數代碼示例

本文整理匯總了Golang中encoding/base32.NewEncoder函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewEncoder函數的具體用法?Golang NewEncoder怎麽用?Golang NewEncoder使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewEncoder函數的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: NewId

// 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()
}
開發者ID:ChrisOHu,項目名稱:platform,代碼行數:11,代碼來源:utils.go

示例2: NewRandomString

func NewRandomString(length int) string {
	var b bytes.Buffer
	str := make([]byte, length+8)
	rand.Read(str)
	encoder := base32.NewEncoder(encoding, &b)
	encoder.Write(str)
	encoder.Close()
	b.Truncate(length) // removes the '==' padding
	return b.String()
}
開發者ID:ChrisOHu,項目名稱:platform,代碼行數:10,代碼來源:utils.go

示例3: Base32ExtEncode

// Base32ExtEncode encodes binary data to base32 extended (RFC 4648) encoded text.
func Base32ExtEncode(data []byte) (text []byte) {
	n := base32.HexEncoding.EncodedLen(len(data))
	buf := bytes.NewBuffer(make([]byte, 0, n))
	encoder := base32.NewEncoder(base32.HexEncoding, buf)
	encoder.Write(data)
	encoder.Close()
	if buf.Len() != n {
		panic("internal error")
	}
	return buf.Bytes()
}
開發者ID:matomesc,項目名稱:rkt,代碼行數:12,代碼來源:strutil.go

示例4: ExampleNewEncoder

func ExampleNewEncoder() {
	input := []byte("foo\x00bar")
	encoder := base32.NewEncoder(base32.StdEncoding, os.Stdout)
	encoder.Write(input)
	// Must close the encoder when finished to flush any partial blocks.
	// If you comment out the following line, the last partial block "r"
	// won't be encoded.
	encoder.Close()
	// Output:
	// MZXW6ADCMFZA====
}
開發者ID:danny8002,項目名稱:go,代碼行數:11,代碼來源:example_test.go

示例5: ExampleNewEncoder1

func ExampleNewEncoder1() {

	// 輸出到標準輸出
	encoder := base32.NewEncoder(base32.StdEncoding, os.Stdout)
	encoder.Write([]byte("this is a test string."))
	// 必須調用Close,刷新輸出
	encoder.Close()

	// Output:
	// ORUGS4ZANFZSAYJAORSXG5BAON2HE2LOM4XA====

}
開發者ID:JamesJiangCHN,項目名稱:gopkg,代碼行數:12,代碼來源:NewEncoder_test.go

示例6: ExampleNewEncoder2

// 輸出到自定義 Writer
func ExampleNewEncoder2() {

	buf := &StringWriter{}

	encoder := base32.NewEncoder(base32.StdEncoding, buf)
	encoder.Write([]byte("this is a test string."))
	encoder.Close()

	fmt.Print(buf.S == "ORUGS4ZANFZSAYJAORSXG5BAON2HE2LOM4XA====")

	// Output:
	// true

}
開發者ID:JamesJiangCHN,項目名稱:gopkg,代碼行數:15,代碼來源:NewEncoder_test.go

示例7: main

func main() {
	flag.Parse()
	var buffer bytes.Buffer
	enc := base32.NewEncoder(encoding(), io.MultiWriter(os.Stdout, &buffer))
	log.Println("encoding to stdout")
	_, err := enc.Write(data())
	enc.Close()
	if err != nil {
		log.Fatalf("failed encoding: %s", err)
	}
	println()
	dec := base32.NewDecoder(encoding(), &buffer)
	log.Println("decoding to stdout")
	io.Copy(os.Stdout, dec)
}
開發者ID:johnvilsack,項目名稱:golang-stuff,代碼行數:15,代碼來源:base32.go

示例8: loginHandler

func loginHandler(w http.ResponseWriter, r *http.Request) {
	err := r.ParseForm()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	username := r.PostForm.Get("username")
	var uid int
	err = db.QueryRow("SELECT UID FROM Users WHERE UPPER(Username)=UPPER(?)", username).Scan(&uid)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	if uid <= 0 {
		http.Error(w, "invalid user id", http.StatusInternalServerError)
		return
	}

	var authtoken string
	{
		var buffer bytes.Buffer
		encoder := base32.NewEncoder(base32.StdEncoding, &buffer)
		_, err = io.CopyN(encoder, rand.Reader, 20)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		err = encoder.Close()
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		authtoken = string(buffer.Bytes())
	}

	_, err = db.Exec("UPDATE Users SET authtoken=? WHERE UID=?", authtoken, uid)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	authtoken = strconv.FormatInt(int64(uid), 10) + "_" + authtoken

	expire := time.Now().AddDate(0, 0, 7)
	const cookieName = "authtoken"
	cookie := http.Cookie{
		Name:       cookieName,
		Value:      authtoken,
		Path:       "/",
		Domain:     "vps.redig.us",
		Expires:    expire,
		RawExpires: expire.Format(time.UnixDate),
		MaxAge:     60 * 60 * 24 * 7,
		Secure:     false,
		HttpOnly:   false,
		Raw:        cookieName + "=" + authtoken,
		Unparsed:   []string{cookieName + "=" + authtoken},
	}
	http.SetCookie(w, &cookie)

	http.Redirect(w, r, "https://vps.redig.us", 303)
}
開發者ID:seemyseoul,項目名稱:uhack,代碼行數:64,代碼來源:main.go


注:本文中的encoding/base32.NewEncoder函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。