git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/jst/README.md (about)

     1  # JSON Secure Token (JST)
     2  
     3  ```
     4  JST_SECRET := base64(crypto.randBytes(64))
     5  secret := base64Decode(JST_SECRET)
     6  encryptionKey := secret[:32]
     7  auhtKey := secret[32:]
     8  
     9  prefix := "jst.v1.local."
    10  
    11  header.nonce := crypto.randBytes(24)
    12  encodedHeader := Base64Url(JSON(header))
    13  
    14  encryptedPayload := XChaCha20-Poly1305.encrypt(key=encryptionKey, data=JSON(payload), nonce=nonce)
    15  encodedPayload := Base64Url(encryptedPayload)
    16  
    17  signature := HMAC-SHA-256(auhtKey, prefix || encodedHeader || "." || encodedPayload)
    18  encodedSignature := Base64Url(signature)
    19  
    20  Token := prefix || encodedHeader || "." || encodedPayload || "." || encodedSignature
    21  ```
    22  
    23  
    24  ```
    25  encryptionContext := "jst-v1 2023-12-31 23:59:59:999 encryption-key"
    26  authenticationContext := "jst-v1 2024-01-01 00:00:00:000 authentication-key"
    27  
    28  jst_master_key := crypto.randBytes(32)
    29  
    30  nonce := crypto.randBytes(24)
    31  encryptionKey := BLAKE3.deriveKey(encryptionContext, jst_master_key)
    32  authenticationKey := BLAKE3.deriveKey(authenticationContext, nonce || jst_master_key)
    33  
    34  encryptedPayload := XChaCha20.encrypt(encryptionKey, nonce, payload)
    35  
    36  signature := BLAKE3.keyed(authenticationKey, [TODO])
    37  
    38  tokenSignature := extractSignature(Token)
    39  signature := HMAC-SHA-256(auhtKey, prefix || encodedHeader || "." || encodedPayload)
    40  
    41  if constantTimeCompare(tokenSignature, signature) == false {
    42      return error;
    43  }
    44  
    45  header := Base64UrlDecode(encodedHeader)
    46  
    47  encryptedPayload := base64UrlDecode(encodedPayload)
    48  decryptedPayload := XChaCha20-Poly1305.decrypt(key=encryptionKey, data=encryptedPayload, nonce=header.nonce)
    49  ```