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


Golang base64.NewEncoder函數代碼示例

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


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

示例1: main

func main() {
	binding := flag.String("b", "post", "Binding. Values are post, redirect, or nourl.")
	flag.Parse()

	input, e := ioutil.ReadAll(os.Stdin)
	if e != nil {
		log.Fatal(e)
	}

	if *binding == "redirect" {
		deflated := deflate(input)

		buf := bytes.Buffer{}
		encoder := base64.NewEncoder(base64.StdEncoding, &buf)
		encoder.Write(deflated)
		encoder.Close()

		fmt.Print(percentURLEncode(buf.String()))

	} else if *binding == "nourl" {
		deflated := deflate(input)

		encoder := base64.NewEncoder(base64.StdEncoding, os.Stdout)
		encoder.Write(deflated)
		encoder.Close()

	} else if *binding == "post" {
		encoder := base64.NewEncoder(base64.StdEncoding, os.Stdout)
		encoder.Write(input)
		encoder.Close()
	} else {
		log.Fatal("Invalid Binding.")
	}

}
開發者ID:kazuyamamoto,項目名稱:samlenc,代碼行數:35,代碼來源:samlenc.go

示例2: SendMulti

// only allows single content
func SendMulti(auth Auth, email MultipartEmail) error {
	if len(email.Content) != 1 {
		panic("only 1 content supported")
	}
	a := smtp.PlainAuth("", auth.User, auth.Password, auth.Host)
	buf := new(bytes.Buffer)
	boundary := randomBoundary()
	header := make(textproto.MIMEHeader)
	header.Set("Subject", email.Subject)
	header.Set("From", email.From)
	header.Set("To", strings.Join(email.To, ", "))
	if len(email.Cc) > 0 {
		header.Set("Cc", strings.Join(email.Cc, ", "))
	}
	header.Set("MIME-Version", "1.0")
	header.Set("Content-Type", "multipart/mixed; boundary="+boundary)
	for k, v := range header {
		for _, s := range v {
			fmt.Fprintf(buf, "%s: %s%s", k, textproto.TrimString(s), crlf)
		}
	}
	fmt.Fprint(buf, crlf)
	mm := multipart.NewWriter(buf)
	mm.SetBoundary(boundary)
	{
		content := email.Content[0]
		header := make(textproto.MIMEHeader)
		header.Set("Content-Type", content.Type)
		header.Set("Content-Transfer-Encoding", "base64")
		part, err := mm.CreatePart(header)
		if err != nil {
			return err
		}
		lw := &lineWriter{Writer: part, Length: 75}
		e := base64.NewEncoder(base64.StdEncoding, lw)
		e.Write(content.Data)
		e.Close()
	}
	for _, a := range email.Attachments {
		header := make(textproto.MIMEHeader)
		header.Set("Content-Type", fmt.Sprintf(`%s; name="%s"`, a.Type, a.Filename))
		if len(a.ContentID) > 0 {
			header.Set("Content-ID", fmt.Sprintf(`<%s>`, a.ContentID))
		}
		header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, a.Filename))
		header.Set("Content-Transfer-Encoding", "base64")
		part, err := mm.CreatePart(header)
		if err != nil {
			return err
		}
		lw := &lineWriter{Writer: part, Length: 75}
		e := base64.NewEncoder(base64.StdEncoding, lw)
		e.Write(a.Data)
		e.Close()
	}
	mm.Close()
	addr := fmt.Sprintf("%s:%d", auth.Host, auth.Port)
	return smtp.SendMail(addr, a, email.From, email.To, buf.Bytes())

}
開發者ID:xoba,項目名稱:goutil,代碼行數:61,代碼來源:smtpc.go

示例3: base64encode

func base64encode(src []byte) string {
	var buf bytes.Buffer
	encoder := base64.NewEncoder(base64.StdEncoding, &buf)
	encoder.Write(src)
	encoder.Close()
	return buf.String()
}
開發者ID:mschlimp,項目名稱:lemming,代碼行數:7,代碼來源:oauth.go

示例4: encodedAuth

func encodedAuth(user, pwd string) string {
	var buf bytes.Buffer
	encoder := base64.NewEncoder(base64.StdEncoding, &buf)
	encoder.Write([]byte(user + ":" + pwd))
	encoder.Close()
	return buf.String()
}
開發者ID:pvulgaris,項目名稱:httpstream,代碼行數:7,代碼來源:stream.go

示例5: signV2

// signV2 sign the request before Do() (AWS Signature Version 2).
func signV2(req http.Request, accessKeyID, secretAccessKey string) *http.Request {
	// Signature calculation is not needed for anonymous credentials.
	if accessKeyID == "" || secretAccessKey == "" {
		return &req
	}

	// Initial time.
	d := time.Now().UTC()

	// Add date if not present.
	if date := req.Header.Get("Date"); date == "" {
		req.Header.Set("Date", d.Format(http.TimeFormat))
	}

	// Calculate HMAC for secretAccessKey.
	stringToSign := getStringToSignV2(req)
	hm := hmac.New(sha1.New, []byte(secretAccessKey))
	hm.Write([]byte(stringToSign))

	// Prepare auth header.
	authHeader := new(bytes.Buffer)
	authHeader.WriteString(fmt.Sprintf("%s %s:", signV2Algorithm, accessKeyID))
	encoder := base64.NewEncoder(base64.StdEncoding, authHeader)
	encoder.Write(hm.Sum(nil))
	encoder.Close()

	// Set Authorization header.
	req.Header.Set("Authorization", authHeader.String())

	return &req
}
開發者ID:ptrimble,項目名稱:dreamhost-personal-backup,代碼行數:32,代碼來源:request-signature-v2.go

示例6: handlePodSign

func handlePodSign(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()

	token := mux.Vars(r)["token"]

	uuid, err := pods.getUUID(token)
	if err != nil {
		w.WriteHeader(http.StatusUnauthorized)
		fmt.Fprintln(w, err)
		return
	}

	content := r.FormValue("content")
	if content == "" {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, "content form value not found")
		return
	}

	// HMAC(UID:content)
	h := hmac.New(sha512.New, hmacKey[:])
	h.Write((*uuid)[:])
	h.Write([]byte(content))

	// Send back HMAC as the signature
	w.Header().Add("Content-Type", "text/plain")
	w.WriteHeader(http.StatusOK)
	enc := base64.NewEncoder(base64.StdEncoding, w)
	enc.Write(h.Sum(nil))
	enc.Close()
}
開發者ID:hwinkel,項目名稱:rkt,代碼行數:31,代碼來源:metadata_service.go

示例7: PutObject

func (c *Client) PutObject(name, bucket string, md5 hash.Hash, size int64, body io.Reader) error {
	req := newReq("http://" + bucket + ".s3.amazonaws.com/" + name)
	req.Method = "PUT"
	req.ContentLength = size
	if md5 != nil {
		b64 := new(bytes.Buffer)
		encoder := base64.NewEncoder(base64.StdEncoding, b64)
		encoder.Write(md5.Sum(nil))
		encoder.Close()
		req.Header.Set("Content-MD5", b64.String())
	}
	c.Auth.SignRequest(req)
	req.Body = ioutil.NopCloser(body)

	res, err := c.httpClient().Do(req)
	if res != nil && res.Body != nil {
		defer res.Body.Close()
	}
	if err != nil {
		return err
	}
	if res.StatusCode != 200 {
		res.Write(os.Stderr)
		return fmt.Errorf("Got response code %d from s3", res.StatusCode)
	}
	return nil
}
開發者ID:JayBlaze420,項目名稱:camlistore,代碼行數:27,代碼來源:client.go

示例8: Base64Encoding

func Base64Encoding(s string) string {
	var buf bytes.Buffer
	encoder := base64.NewEncoder(base64.StdEncoding, &buf)
	defer encoder.Close()
	encoder.Write([]byte(s))
	return buf.String()
}
開發者ID:rose312,項目名稱:mzr,代碼行數:7,代碼來源:helper.go

示例9: CalcBase64

func CalcBase64(data string) string {
	var buf bytes.Buffer
	encoder := base64.NewEncoder(base64.StdEncoding, &buf)
	encoder.Write([]byte(data))
	encoder.Close()
	return buf.String()
}
開發者ID:WH-Wang,項目名稱:libgo,代碼行數:7,代碼來源:utils.go

示例10: Encode

// Encode encodes the transport as a single string for the purpose of letting a
// user paste it into his application configuraion.
func (t *Connection) Encode() (string, error) {
	resultWriter := new(bytes.Buffer)

	// Write version number first and verify that it wrote exactly two characters
	n, _ := resultWriter.WriteString(ShareableTansportConnectionVersion)
	if n != 2 {
		panic("invalid version lentgh")
	}

	data := struct {
		Transport string `json:"t"`
		Secret    string `json:"s"`
		Addr      string `json:"a"`
	}{
		Transport: t.Transport,
		Secret:    t.Secret,
		Addr:      t.Addr,
	}
	// JSON encode the data struct
	jsonBytes, err := json.Marshal(data)
	if err != nil {
		panic(err)
	}

	// Base64 encode the JSON data
	encoder := base64.NewEncoder(base64.URLEncoding, resultWriter)
	encoder.Write(jsonBytes)
	encoder.Close()

	return string(resultWriter.Bytes()[:]), nil
}
開發者ID:thomasf,項目名稱:alkasir,代碼行數:33,代碼來源:connection.go

示例11: writeBase64String

func writeBase64String(s string, b *bufio.Writer, f *Format) {
  b.WriteRune('|')
  w := base64.NewEncoder(base64.StdEncoding, b)
  w.Write([]byte(s))
  w.Close()
  b.WriteRune('|')
}
開發者ID:hlandau,項目名稱:sx,代碼行數:7,代碼來源:sx.go

示例12: PutObject

func (c *Client) PutObject(name, bucket string, md5 hash.Hash, size int64, body io.Reader) error {
	req := newReq("http://" + bucket + "." + c.hostname() + "/" + name)
	req.Method = "PUT"
	req.ContentLength = size
	if md5 != nil {
		b64 := new(bytes.Buffer)
		encoder := base64.NewEncoder(base64.StdEncoding, b64)
		encoder.Write(md5.Sum(nil))
		encoder.Close()
		req.Header.Set("Content-MD5", b64.String())
	}
	if c.DefaultACL != "" {
		req.Header.Set("x-amz-acl", c.DefaultACL)
	}
	contentType := mime.TypeByExtension(path.Ext(name))
	if contentType == "" {
		contentType = "application/octet-stream"
	}
	req.Header.Set("Content-Type", contentType)
	c.Auth.SignRequest(req)
	req.Body = ioutil.NopCloser(body)

	res, err := c.httpClient().Do(req)
	if res != nil && res.Body != nil {
		defer httputil.CloseBody(res.Body)
	}
	if err != nil {
		return err
	}
	if res.StatusCode != http.StatusOK {
		res.Write(os.Stderr)
		return fmt.Errorf("Got response code %d from s3", res.StatusCode)
	}
	return nil
}
開發者ID:simonz05,項目名稱:util,代碼行數:35,代碼來源:client.go

示例13: encode64

func encode64(value string) (result string) {
	buffer := &bytes.Buffer{}
	encoder := base64.NewEncoder(base64.StdEncoding, buffer)
	encoder.Write([]byte(value))
	encoder.Close()
	return buffer.String()
}
開發者ID:qrush,項目名稱:higo,代碼行數:7,代碼來源:sessions.go

示例14: Encode

// Encode encodes data to a base64 encoded using the secconf codec.
// data is encrypted with all public keys found in the supplied keyring.
func Encode(data []byte, keyring io.Reader) ([]byte, error) {
	entityList, err := openpgp.ReadArmoredKeyRing(keyring)
	if err != nil {
		return nil, err
	}
	buffer := new(bytes.Buffer)
	encoder := base64.NewEncoder(base64.StdEncoding, buffer)
	pgpWriter, err := openpgp.Encrypt(encoder, entityList, nil, nil, nil)
	if err != nil {
		return nil, err
	}
	gzWriter := gzip.NewWriter(pgpWriter)
	if _, err := gzWriter.Write(data); err != nil {
		return nil, err
	}
	if err := gzWriter.Close(); err != nil {
		return nil, err
	}
	if err := pgpWriter.Close(); err != nil {
		return nil, err
	}
	if err := encoder.Close(); err != nil {
		return nil, err
	}
	return buffer.Bytes(), nil
}
開發者ID:untoldone,項目名稱:bloomapi,代碼行數:28,代碼來源:secconf.go

示例15: wrap64

// wrap64 writes a byte payload as wrapped base64 to an io.writer
func wrap64(writer io.Writer, b []byte, wrap int) {
	breaker := NewLineBreaker(writer, wrap)
	b64 := base64.NewEncoder(base64.StdEncoding, breaker)
	b64.Write(b)
	b64.Close()
	breaker.Close()
}
開發者ID:crooks,項目名稱:yamn,代碼行數:8,代碼來源:linebreaker_test.go


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