/PsJwt

Primary LanguagePowerShell

PSJwt

快速使用1

& {
    irm 'raw.githubusercontent.com/hunandy14/PsJwt/main/PSJwt/PSJwt.github.psm1' |iex
    $data = ConvertTo-JwtUnsignToken ([ordered]@{alg="RS512";typ="JWT"}) @{email="jordan@example.com"}
    $signature = ConvertTo-Base64Url($data |icb OpenSSL dgst -sha512 -sign .\key\private_key.pem)
    Write-Host "$data.$signature" -ForegroundColor DarkGreen
}

關於 OpenSSL

  1. 第三方編譯的安裝檔案 : https://slproweb.com/products/Win32OpenSSL.html
  2. 第三方 choco 快速安裝: choco install -y openssl

對於其他加密算法對應的 OpenSSL Command 指令請參照這份表格
https://github.com/hunandy14/PsJwt/blob/main/OpenSSL/AlgorithmMappingTable.md




線上載入

irm 'raw.githubusercontent.com/hunandy14/PsJwt/main/PSJwt/PSJwt.github.psm1' |iex

線上載入Proxy ($env:HTTP_PROXY = 'http://YOUR_PROXY:8080')

irm 'raw.githubusercontent.com/hunandy14/PsJwt/main/PSJwt/PSJwt.github.psm1' -proxy $env:HTTP_PROXY |iex

本地檔案

Import-Module (Join-Path $PSScriptRoot ".\PSJwt\PSJwt.psm1") -Force -ErrorAction Stop

快速使用2

# 簽名用私鑰
$privatekeyPath = '.\key\private_key.pem'

# 生成 JWT 用的 JSON 文字 (包含換行)
$header = @{
    alg = "RS512"
    typ = "JWT"
} | ConvertTo-Json -Compress
$payload = @{
    email = "jordan@example.com"
} | ConvertTo-Json -Compress

# 使用 JSON 文字生成 JWT 聲明字符串
$jwtDataToken = ConvertTo-JwtUnsignToken -HeaderString $header -PayloadString $payload

# 對 JWT 聲明字符串簽名
$byte = $jwtDataToken | 
    Invoke-CommandAndGetBinaryOutput OpenSSL dgst -sha512 -binary -sign $privatekeyPath
$signature = ConvertTo-Base64Url($byte)

# 組合成最終的 JWT
$jwt = "$jwtDataToken.$signature"
Write-Host $jwt -ForegroundColor DarkGreen




測試

簽名測試測試用網站: https://jwt.io/

進入之後把下面JWT跟公鑰填入,可以看到左下角有個 Signature Verified 就是驗證功過了
也可以填入私鑰後直接修改右邊的 PAYLOAD 網站會自動生成正確的 JWT 字串


快速使用範例預期產出的 JWT 結果

eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImpvcmRhbkBleGFtcGxlLmNvbSJ9.Sh6zZXY_q-CZiDOIaxQEraqIh0gQr24jhsTRZ2OWv7NpxOzdECJzV2Lbw_sSngOkjMtMuw5pztixoBNIwxli1aFIE5pxTEgOZ2faIQQ7iCVmBERNGvoLxvfF0ClhMxHGnrPam8Q_hRcDsgl-uGC4-snMrx7-b5eDLJC14cHEfpkgIbzA65JGxqpycF-oy757t3B5DQcZQkE-XjeJk-qlxiX4Qq_Ez8hxUxRiy4ysAxYgHIOhukYivvmsVdghCpU5wD_gEEjO6NeTuwLCqtl9k5XP-LByIo8eXermD2BmsLYkSh4mYk68FqwnoEGYFIknVdKJPZ5n5GAjd1vD0lssmw

測試用公鑰 .\key\public_key.pem

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyo8/5NIZNSF8LvXUbzrG
nyKEolcVMsieWmdX+oLwQl2zPb5s86J031Z2qRwuVFHDIUyih9YJaV1gFjy0QC+0
ISYIdDGvmyetAVXNIEagGuNkvL+YecX3vVH7+gARh9ao17sNuwquDF6gO6n5kKrE
3FAWbExB2R9QOeXnYb8TeYjZNce1JKHEHe4+GG5oJODY7zk9OKCuhOC+SrA05rBJ
CE3CeNxPIliZeJ/eIoxvjF82qBWKHIzlIcY7i5ndZqVtPk/ycBWIOXmnBLPyGRDX
mIgbiQ7Pt4P1fdgx2I6bctuWGbxlN0SDAIjcNvIgZylBVaL8/WQ5eCNegFNxFc8w
OQIDAQAB
-----END PUBLIC KEY-----

測試用私鑰 .\key\private_key.pem

-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDKjz/k0hk1IXwu
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