2
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 1 year has passed since last update.

paiza.ioでelixirAdvent Calendar 2022

Day 14

paiza.ioでelixir その14

Last updated at Posted at 2022-10-26

概要

paiza.ioでelixirやってみた。
jwtの検証やってみた。

検証したjwt

  • 出所
    wikiペディア

  • 全体

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dnZWRJbkFzIjoiYWRtaW4iLCJpYXQiOjE0MjI3Nzk2Mzh9.gzSraSYS8EXBxLN_oWnFSRgCzcmJmMjLiuyu5CSpyHI
  • タイプ
    HS256

  • キー
    secretkey

  • ヘッダー
    {
    "alg": "HS256",
    "typ": "JWT"
    }

  • ペイロード
    {
    "loggedInAs":"admin",
    "iat":1422779638
    }

  • シグニチャー
    gzSraSYS8EXBxLN_oWnFSRgCzcmJmMjLiuyu5CSpyHI

結果

{"alg":"HS256","typ":"JWT"}
{"loggedInAs":"admin","iat":1422779638}
gzSraSYS8EXBxLN_oWnFSRgCzcmJmMjLiuyu5CSpyHI=

サンプルコード

defmodule Jwt do
	def decode(str) do
	    [head, payload, _] = String.split(str, ".") 
	    head = String.replace(head, "-", "+")
	    head = String.replace(head, "_", "/")
	    {:ok, res} = Base.decode64 head
        res
        |> IO.puts
        payload = String.replace(payload, "-", "+")
	    payload = String.replace(payload, "_", "/")
	    {:ok, res} = Base.decode64 payload
        res
        |> IO.puts
	end
    def enc(str) do
        :crypto.mac(:hmac, :sha256, "secretkey", str)
        |> Base.encode64( case: :lower)
        |> String.replace("+", "-")
	    |> String.replace("/", "_")
        |> IO.puts
    end
end


Jwt.decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dnZWRJbkFzIjoiYWRtaW4iLCJpYXQiOjE0MjI3Nzk2Mzh9.gzSraSYS8EXBxLN_oWnFSRgCzcmJmMjLiuyu5CSpyHI")
Jwt.enc("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dnZWRJbkFzIjoiYWRtaW4iLCJpYXQiOjE0MjI3Nzk2Mzh9")





成果物

以上。

2
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
2
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?