예제 #1
0
파일: utils.go 프로젝트: ChrisOHu/platform
// 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()
}
예제 #2
0
파일: utils.go 프로젝트: ChrisOHu/platform
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()
}
예제 #3
0
파일: strutil.go 프로젝트: matomesc/rkt
// 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()
}
예제 #4
0
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====
}
예제 #5
0
func ExampleNewEncoder1() {

	// 输出到标准输出
	encoder := base32.NewEncoder(base32.StdEncoding, os.Stdout)
	encoder.Write([]byte("this is a test string."))
	// 必须调用Close,刷新输出
	encoder.Close()

	// Output:
	// ORUGS4ZANFZSAYJAORSXG5BAON2HE2LOM4XA====

}
예제 #6
0
// 输出到自定义 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

}
예제 #7
0
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)
}
예제 #8
0
파일: main.go 프로젝트: seemyseoul/uhack
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)
}