jonasroussel / dart_jsonwebtoken

A dart implementation of the famous javascript library 'jsonwebtoken'
MIT License
87 stars 29 forks source link

Use PrivateKey of size 32 instead of 64 #15

Closed JonasHiltl closed 3 years ago

JonasHiltl commented 3 years ago

I'm generating Keypairs on a different system and send them to the client device. My Keys (pub + priv) both have a size of 32 and are base58 encoded. After decoding them with bs58 I want to create a JWT with the private key and decode the JWT on a node.js backend.

final secret = "F2ZNsmXYwLsryvhVVXnqEoWM2Umro6vtWPQCyEg7oj8P"

generateJwt(String subject, String secret) {
  final decodedRaw = Base58Decode(secret);
  /* [208, 105, 206, 135, 43, 101, 101, 250, 227, 140, 174, 15, 170, 99, 69, 156,
   193, 74, 234, 158, 136, 83, 124, 133, 190, 248, 205, 196, 217, 126, 164, 196] */

  final jwt = JWT(
    {
      'subject': subject,
    },
    issuer: subject,
  );

  final key = EdDSAPrivateKey(decodedRaw);
  // JWTUndefinedError: Invalid argument(s): ed25519: bad privateKey length 32

  final token = jwt.sign(key, algorithm: JWTAlgorithm.EdDSA);

  print('Signed token: $token\n');
}

When I try to sign the JWT with my private key I get the error : JWTUndefinedError: Invalid argument(s): ed25519: bad privateKey length 32. Is it possible to use a private key of size 32 to sign a JWT?

JonasHiltl commented 3 years ago

I found out that the required 64 bytes key is produced by concatenating the private and public key. My SO question