本文整理汇总了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;未经允许,请勿转载。 |
请发表评论