This commit is contained in:
Jonas Roussel
2023-08-23 12:34:24 +02:00
parent 1e69c815c5
commit 3847ffcca1
9 changed files with 314 additions and 36 deletions

View File

@ -11,9 +11,17 @@ void main() {
rs256();
print('-----------------------\n');
print('----- ECDSA SHA-256 -----');
print('----- ECDSA P-256 -----');
es256();
print('-------------------------');
print('-----------------------\n');
print('----- ECDSA secp256k -----');
es256k();
print('--------------------------\n');
print('----- RSA-PSS SHA-256 -----');
ps256();
print('---------------------------\n');
}
// HMAC SHA-256 algorithm
@ -96,7 +104,7 @@ void rs256() {
}
}
// ECDSA SHA-256 algorithm
// ECDSA P-256 algorithm
void es256() {
String token;
@ -138,3 +146,89 @@ void es256() {
}
}
}
// ECDSA secp256k algorithm
void es256k() {
String token;
/* Sign */ {
// Create a json web token
final jwt = JWT(
{
'id': 123,
'server': {
'id': '3e4fc296',
'loc': 'euw-2',
}
},
issuer: 'https://github.com/jonasroussel/dart_jsonwebtoken',
);
// Sign it
final pem = File('./example/ec_256k_private.pem').readAsStringSync();
final key = ECPrivateKey(pem);
token = jwt.sign(key, algorithm: JWTAlgorithm.ES256K);
print('Signed token: $token\n');
}
/* Verify */ {
try {
// Verify a token
final pem = File('./example/ec_256k_public.pem').readAsStringSync();
final key = ECPublicKey(pem);
final jwt = JWT.verify(token, key);
print('Payload: ${jwt.payload}');
} on JWTExpiredException {
print('jwt expired');
} on JWTException catch (ex) {
print(ex.message); // ex: invalid signature
}
}
}
// RSA-PSS SHA-256 algorithm
void ps256() {
String token;
/* Sign */ {
// Create a json web token
final jwt = JWT(
{
'id': 123,
'server': {
'id': '3e4fc296',
'loc': 'euw-2',
}
},
issuer: 'https://github.com/jonasroussel/dart_jsonwebtoken',
);
// Sign it
final pem = File('./example/rsa_pss_private.pem').readAsStringSync();
final key = RSAPrivateKey(pem);
token = jwt.sign(key, algorithm: JWTAlgorithm.PS256);
print('Signed token: $token\n');
}
/* Verify */ {
try {
// Verify a token
final pem = File('./example/rsa_pss_public.pem').readAsStringSync();
final key = RSAPublicKey(pem);
final jwt = JWT.verify(token, key);
print('Payload: ${jwt.payload}');
} on JWTExpiredException {
print('jwt expired');
} on JWTException catch (ex) {
print(ex.message); // ex: invalid signature
}
}
}