base64

package
v1.21.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2023 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package base64は、RFC 4648で指定されているように、base64エンコーディングを実装します。

Example
package main

import (
	"github.com/shogo82148/std/encoding/base64"
	"github.com/shogo82148/std/fmt"
)

func main() {
	msg := "Hello, 世界"
	encoded := base64.StdEncoding.EncodeToString([]byte(msg))
	fmt.Println(encoded)
	decoded, err := base64.StdEncoding.DecodeString(encoded)
	if err != nil {
		fmt.Println("decode error:", err)
		return
	}
	fmt.Println(string(decoded))
}
Output:

SGVsbG8sIOS4lueVjA==
Hello, 世界

Index

Examples

Constants

View Source
const (
	StdPadding rune = '='
	NoPadding  rune = -1
)

Variables

View Source
var RawStdEncoding = StdEncoding.WithPadding(NoPadding)

RawStdEncodingは、RFC 4648セクション3.2で定義されている標準の生の、パディングされていないbase64エンコーディングです。 これは、StdEncodingと同じですが、パディング文字が省略されています。

View Source
var RawURLEncoding = URLEncoding.WithPadding(NoPadding)

RawURLEncodingは、RFC 4648で定義されているパディングされていない代替のbase64エンコーディングです。 通常、URLやファイル名で使用されます。 これは、URLEncodingと同じですが、パディング文字が省略されています。

View Source
var StdEncoding = NewEncoding(encodeStd)

StdEncodingは、RFC 4648で定義されている標準のbase64エンコーディングです。

View Source
var URLEncoding = NewEncoding(encodeURL)

URLEncodingは、RFC 4648で定義されている代替のbase64エンコーディングです。 通常、URLやファイル名で使用されます。

Functions

func NewDecoder

func NewDecoder(enc *Encoding, r io.Reader) io.Reader

NewDecoderは、新しいbase64ストリームデコーダーを構築します。

func NewEncoder

func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser

NewEncoderは、新しいbase64ストリームエンコーダーを返します。 返されたライターに書き込まれたデータはencを使用してエンコードされ、その後wに書き込まれます。 Base64エンコーディングは4バイトブロックで動作します。 書き込みが完了したら、呼び出し元は、部分的に書き込まれたブロックをフラッシュするために返されたエンコーダーを閉じる必要があります。

Example
package main

import (
	"github.com/shogo82148/std/encoding/base64"
	"github.com/shogo82148/std/os"
)

func main() {
	input := []byte("foo\x00bar")
	encoder := base64.NewEncoder(base64.StdEncoding, os.Stdout)
	encoder.Write(input)
	// 部分的なブロックをフラッシュするために、終了時にエンコーダーを閉じる必要があります。
	// 次の行のコメントを外すと、最後の部分ブロック「r」がエンコードされなくなります。
	encoder.Close()
}
Output:

Zm9vAGJhcg==

Types

type CorruptInputError

type CorruptInputError int64

func (CorruptInputError) Error

func (e CorruptInputError) Error() string

type Encoding

type Encoding struct {
	// contains filtered or unexported fields
}

Encodingは、64文字のアルファベットによって定義される基数64のエンコーディング/デコーディングスキームです。 最も一般的なものは、RFC 4648で定義され、MIME(RFC 2045)およびPEM(RFC 1421)で使用される「base64」エンコーディングです。 RFC 4648は、+と/の代わりに-と_が使用された標準エンコーディングを定義しています。

func NewEncoding

func NewEncoding(encoder string) *Encoding

NewEncodingは、与えられたアルファベットによって定義されるパディングされた新しいエンコーディングを返します。 アルファベットは、パディング文字またはCR / LF('\r'、'\n')を含まない64バイトの文字列でなければなりません。 アルファベットは、マルチバイトUTF-8に対する特別な処理なしに、バイト値のシーケンスとして扱われます。 結果のエンコーディングは、デフォルトのパディング文字('=')を使用します。 パディング文字はWithPaddingを介して変更または無効化できます。

func (*Encoding) Decode

func (enc *Encoding) Decode(dst, src []byte) (n int, err error)

Decodeは、エンコーディングencを使用してsrcをデコードし、 DecodedLen(len(src))バイトをdstに書き込みます。 srcに無効なbase64データが含まれている場合、 書き込まれたバイト数とCorruptInputErrorを返します。 改行文字(\rおよび\n)は無視されます。

Example
package main

import (
	"github.com/shogo82148/std/encoding/base64"
	"github.com/shogo82148/std/fmt"
)

func main() {
	str := "SGVsbG8sIHdvcmxkIQ=="
	dst := make([]byte, base64.StdEncoding.DecodedLen(len(str)))
	n, err := base64.StdEncoding.Decode(dst, []byte(str))
	if err != nil {
		fmt.Println("decode error:", err)
		return
	}
	dst = dst[:n]
	fmt.Printf("%q\n", dst)
}
Output:

"Hello, world!"

func (*Encoding) DecodeString

func (enc *Encoding) DecodeString(s string) ([]byte, error)

DecodeStringは、base64文字列sによって表されるバイト列を返します。

Example
package main

import (
	"github.com/shogo82148/std/encoding/base64"
	"github.com/shogo82148/std/fmt"
)

func main() {
	str := "c29tZSBkYXRhIHdpdGggACBhbmQg77u/"
	data, err := base64.StdEncoding.DecodeString(str)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	fmt.Printf("%q\n", data)
}
Output:

"some data with \x00 and \ufeff"

func (*Encoding) DecodedLen

func (enc *Encoding) DecodedLen(n int) int

DecodedLenは、nバイトのbase64エンコードされたデータに対応するデコードされたデータの最大バイト数を返します。

func (*Encoding) Encode

func (enc *Encoding) Encode(dst, src []byte)

Encodeは、エンコーディングencを使用してsrcをエンコードし、 EncodedLen(len(src))バイトをdstに書き込みます。

エンコーディングは、出力を4バイトの倍数にパディングするため、 大量のデータストリームの個々のブロックにEncodeを使用することは適切ではありません。 代わりにNewEncoder()を使用してください。

Example
package main

import (
	"github.com/shogo82148/std/encoding/base64"
	"github.com/shogo82148/std/fmt"
)

func main() {
	data := []byte("Hello, world!")
	dst := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
	base64.StdEncoding.Encode(dst, data)
	fmt.Println(string(dst))
}
Output:

SGVsbG8sIHdvcmxkIQ==

func (*Encoding) EncodeToString

func (enc *Encoding) EncodeToString(src []byte) string

EncodeToStringは、srcのbase64エンコーディングを返します。

Example
package main

import (
	"github.com/shogo82148/std/encoding/base64"
	"github.com/shogo82148/std/fmt"
)

func main() {
	data := []byte("any + old & data")
	str := base64.StdEncoding.EncodeToString(data)
	fmt.Println(str)
}
Output:

YW55ICsgb2xkICYgZGF0YQ==

func (*Encoding) EncodedLen

func (enc *Encoding) EncodedLen(n int) int

EncodedLenは、長さnの入力バッファのbase64エンコーディングのバイト数を返します。

func (Encoding) Strict added in v1.8.0

func (enc Encoding) Strict() *Encoding

Strictは、RFC 4648セクション3.5で説明されているように、 末尾のパディングビットがゼロであることを要求する厳密なデコードが有効になっているencと同一の新しいエンコーディングを作成します。

ただし、入力はまだ操作可能であり、改行文字(CRおよびLF)は引き続き無視されます。

func (Encoding) WithPadding added in v1.5.0

func (enc Encoding) WithPadding(padding rune) *Encoding

WithPaddingは、指定されたパディング文字またはNoPaddingを使用して、encと同一の新しいエンコーディングを作成します。 パディング文字は'\r'または'\n'ではなく、エンコーディングのアルファベットに含まれていない必要があり、'\xff'以下のルーンである必要があります。 '\x7f'より上のパディング文字は、コードポイントのUTF-8表現を使用する代わりに、その正確なバイト値としてエンコードされます。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL