base32

package
v1.23.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

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

Index

Examples

Constants

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

Variables

View Source
var HexEncoding = NewEncoding(encodeHex)

HexEncodingは、RFC 4648で定義されている「Extended Hex Alphabet」です。 通常、DNSで使用されます。

View Source
var StdEncoding = NewEncoding(encodeStd)

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

Functions

func NewDecoder

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

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

func NewEncoder

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

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

Example
package main

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

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

MZXW6ADCMFZA====

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は、32文字のアルファベットによって定義される基数32のエンコーディング/デコーディングスキームです。 最も一般的なものは、SASL GSSAPIで導入され、RFC 4648で標準化された「base32」エンコーディングです。 代替の「base32hex」エンコーディングは、DNSSECで使用されます。

func NewEncoding

func NewEncoding(encoder string) *Encoding

NewEncodingは、与えられたアルファベットで定義されたパディングされたエンコーディングを返します。 アルファベットは、パディング文字またはCR / LF('\r'、'\n')を含まず、一意のバイト値を含む32バイトの文字列である必要があります。 アルファベットは、マルチバイトUTF-8の特別な処理なしにバイト値のシーケンスとして扱われます。 結果のエンコーディングは、デフォルトのパディング文字('=')を使用します。 パディング文字を変更または無効にするには、Encoding.WithPadding を使用できます。

func (*Encoding) AppendDecode added in v1.22.0

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

AppendDecodeは、base32でデコードされたsrcをdstに追加し、 拡張されたバッファを返します。 入力が不正な形式の場合、部分的にデコードされたsrcとエラーを返します。

func (*Encoding) AppendEncode added in v1.22.0

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

AppendEncodeは、base32でエンコードされたsrcをdstに追加し、 拡張されたバッファを返します。

func (*Encoding) Decode

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

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

Example
package main

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

func main() {
	str := "JBSWY3DPFQQHO33SNRSCC==="
	dst := make([]byte, base32.StdEncoding.DecodedLen(len(str)))
	n, err := base32.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は、base32文字列sによって表されるバイト列を返します。

Example
package main

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

func main() {
	str := "ONXW2ZJAMRQXIYJAO5UXI2BAAAQGC3TEEDX3XPY="
	data, err := base32.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バイトのbase32エンコードされたデータに対応するデコードされたデータの最大バイト数を返します。

func (*Encoding) Encode

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

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

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

Example
package main

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

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

JBSWY3DPFQQHO33SNRSCC===

func (*Encoding) EncodeToString

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

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

Example
package main

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

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

MFXHSIBLEBXWYZBAEYQGIYLUME======

func (*Encoding) EncodedLen

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

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

func (Encoding) WithPadding added in v1.9.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