JWT
Posted on October 2, 2021
Tags: javascript
1 Hash
- FUNCTION:
crypto.subtle.digest("SHA-1",data)
- options are
"SHA-1"
"SHA-256"
"SHA-384"
"SHA-512"
- options are
- Like SHA-256 or MD5 hash files to check INTEGRITY that files sent are not tampered
const text =
"An obscure body in the S-K System, your majesty. The inhabitants refer to it as the planet Earth.";
async function digestMessage(message) {
const encoder = new TextEncoder();
const data = encoder.encode(message);
const hash = await crypto.subtle.digest("SHA-256", data);
return hash;
}
digestMessage(text).then((digestBuffer) =>
console.log(digestBuffer.byteLength),
);
2 HMAC vs Hash
- INPUT: Private key generation algorithm, someFile
- HMAC is just the hashing using a seed derived from a private key.
- The private key gives AUTHENTICITY that files came from a sender that holds some private key.
- Because only the sender can generate this Hash
- HMAC also gives INTEGRITY like typical Hash functions do
3 HS256
- HS256 is just HMAC using SHA256
let key = await window.crypto.subtle.generateKey(
{
name: "HMAC",
hash: { name: "SHA-256" },
},
true,
["sign", "verify"],
);
4 RS256
- RS256 (RSA using SHA256)
5 Algorithms
- For Symmetric algorithms, they generate a
CryptoKey
Object - For Asymmetric algorithms, they generate a
CryptoKeyPair
Object.sampleKeyPair.privateKey
andsampleKeyPair.publicKey
areCryptoKey
Objects derived from theCryptoKeyPair
Object- asymmetric algorithms: RSASSA-PKCS1-v1_5, RSA-PSS, RSA-OAEP, ECDSA, or ECDH.
- Note: Assymmetric algorithms are commonly known as Public-key cryptography
- Commonly used public-key cryptosystems are RSA (for both signing and encryption), DSA (for signing) and Diffie-Hellman (for key agreement)
Algorithm | sign()verify() | encrypt()decrypt() | digest() | deriveBits()deriveKey() | generateKey() | wrapKey()unwrapKey() | exportKey() | importKey() |
---|---|---|---|---|---|---|---|---|
RSASSA PKCS1 v1.5* | ✓ | ✓ | ✓ | ✓ | ||||
RSA PSS* | ✓ | ✓ | ✓ | ✓ | ||||
RSA OAEP* | ✓ | ✓ | ✓ | ✓ | ✓ | |||
ECDSA* | ✓ | ✓ | ✓ | ✓ | ||||
ECDH* | ✓ | ✓ | ✓ | ✓ | ||||
Ed255191 | ✓ | ✓ | ✓ | ✓ | ||||
X255191 | ✓ | ✓ | ✓ | ✓ | ||||
NODE ED255192 | ✓ | ✓ | ✓ | ✓ | ||||
AES CTR | ✓ | ✓ | ✓ | ✓ | ✓ | |||
AES CBC | ✓ | ✓ | ✓ | ✓ | ✓ | |||
AES GCM | ✓ | ✓ | ✓ | ✓ | ✓ | |||
AES KW | ✓ | ✓ | ✓ | ✓ | ||||
HMAC | ✓ | ✓ | ✓ | ✓ | ||||
SHA 1 | ✓ | |||||||
SHA 256 | ✓ | |||||||
SHA 384 | ✓ | |||||||
SHA 512 | ✓ | |||||||
MD53 | ✓ | |||||||
HKDF | ✓ | ✓ | ||||||
PBKDF2 | ✓ | ✓ |