1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Go: JWT のペイロード部をデコード

Last updated at Posted at 2021-02-07

こちらのプログラムを Go で書き換えました。
Node.js: JWT のペイロード部をデコード
Python3: JWT のペイロード部をデコード

decode_jwt.go
// ---------------------------------------------------------------
//
//	decode_jwt.go
//
//				  Feb/07/2021
// ---------------------------------------------------------------
package main

import (
	"fmt"
	"os"
	"strings"
	"io/ioutil"
	b64 "encoding/base64"
)

// ---------------------------------------------------------------
func decode_jwt_proc(str_token string) string {
	aaa := strings.Split (str_token,".")
	str_bbb := strings.Replace(aaa[1],"-","+",-1)
	str_ccc := strings.Replace(str_bbb,"_","/",-1)

	llx := len(str_ccc)
	nnx := ((4 - llx % 4) % 4)
	ssx := strings.Repeat("=" , nnx)
	str_ddd := strings.Join([]string{str_ccc,ssx},"")
	ppp, err :=  b64.StdEncoding.DecodeString(str_ddd)
	if err != nil {
		fmt.Fprintf (os.Stderr,"*** error *** StdEncoding.DecodeString ***\n")
		fmt.Println("error:", err)
		return "error"
	}

	uEnc := b64.URLEncoding.EncodeToString([]byte(ppp))
	decode, _ := b64.URLEncoding.DecodeString(uEnc)

	return string(decode)
}

// ---------------------------------------------------------------
func main() {

	fmt.Fprintf (os.Stderr,"*** 開始 ***\n")
	file_token := os.Args[1]
	fmt.Fprintf (os.Stderr,"file_token = " + file_token + "\n")

	buff,_ := ioutil.ReadFile(file_token)
	fmt.Fprintf (os.Stderr,"len(buff) = %d\n" , len(buff))

	json_str := decode_jwt_proc(string(buff))
	fmt.Println (json_str)

	fmt.Fprintf (os.Stderr,"*** 終了 ***\n")
}

// ---------------------------------------------------------------

実行方法

go run decode_jwt.go token01.txt | jq .
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?