【发布时间】:2019-09-05 17:07:59
【问题描述】:
RFC2045 第 6.8 节规定 base64 输出的最大编码行长度应为 76 个字符或更少。
Golang 流编写器 base64.NewEncoder 没有任何用于行拆分的选项,如这里所示。
package main
import (
"encoding/base64"
"io"
"os"
"strings"
)
// See https://www.ietf.org/rfc/rfc2045.txt, section 6.8 for notes on maximum line length of 76 characters
func main() {
data := "It is only the hairs on a gooseberry that prevent it from being a grape! This is long enough to need a line split"
rdr := strings.NewReader(data)
wrt := base64.NewEncoder(base64.StdEncoding, os.Stdout)
io.Copy(wrt, rdr)
}
输出是
SXQgaXMgb25seSB0aGUgaGFpcnMgb24gYSBnb29zZWJlcnJ5IHRoYXQgcHJldmVudCBpdCBmcm9tIGJlaW5nIGEgZ3JhcGUhIEl0IGlzIG9ubHkgdGhlIGhhaXJzIG9uIGEgZ29vc2ViZXJyeSB0aGF0IHByZXZlbnQgaXQgZnJvbSBiZWluZyBhIGdyYXBl
是否有基于流的解决方案来分割线? MIME library 仅提供基于字符串的编码选项。
【问题讨论】:
-
不是基于流的,但也许可以使用 encoding/pem 破解某些东西