Base64エンコーディング
Base64エンコーディング
Section titled “Base64エンコーディング”バイナリデータをbase64文字列にエンコードし、base64をバイナリにデコード。RFC 4648に準拠した標準base64エンコーディングを使用。
local base64 = require("base64")エンコーディング
Section titled “エンコーディング”データのエンコード
Section titled “データのエンコード”文字列(バイナリデータを含む)をbase64にエンコード。
-- テキストをエンコードlocal encoded = base64.encode("Hello, World!")print(encoded) -- "SGVsbG8sIFdvcmxkIQ=="
-- バイナリデータをエンコード(例: ファイルから)local image_data = fs.read_binary("photo.jpg")local image_b64 = base64.encode(image_data)
-- 転送用にJSONをエンコードlocal json = require("json")local payload = json.encode({user = "alice", action = "login"})local token_part = base64.encode(payload)
-- 認証情報をエンコードlocal credentials = base64.encode("username:password")local auth_header = "Basic " .. credentials| パラメータ | 型 | 説明 |
|---|---|---|
data | string | エンコードするデータ(テキストまたはバイナリ) |
戻り値: string, error - 空文字列入力は空文字列を返す。
デコーディング
Section titled “デコーディング”データのデコード
Section titled “データのデコード”base64文字列を元のデータにデコード。
-- テキストをデコードlocal decoded = base64.decode("SGVsbG8sIFdvcmxkIQ==")print(decoded) -- "Hello, World!"
-- エラー処理付きでデコードlocal data, err = base64.decode(user_input)if err then return nil, errors.new("INVALID", "Invalid base64 data")end
-- バイナリデータをデコードlocal image_b64 = request.bodylocal image_data, err = base64.decode(image_b64)if err then return nil, errendfs.write_binary("output.jpg", image_data)
-- JWTパーツをデコードlocal parts = string.split(jwt_token, ".")local header = json.decode(base64.decode(parts[1]))local payload = json.decode(base64.decode(parts[2]))| パラメータ | 型 | 説明 |
|---|---|---|
data | string | Base64エンコードされた文字列 |
戻り値: string, error - 空文字列入力は空文字列を返す。
| 条件 | 種別 | 再試行可能 |
|---|---|---|
| 入力が文字列ではない | errors.INVALID | no |
| 無効なbase64文字 | errors.INVALID | no |
| 破損したパディング | errors.INVALID | no |
エラーの処理についてはエラー処理を参照。