createJWT function

String createJWT(
  1. Map<String, dynamic> payload,
  2. String secret
)

Implementation

String createJWT(Map<String, dynamic> payload, String secret) {
  final header = {
    'alg': 'HS256',
    'typ': 'JWT',
  };

  final base64UrlHeader = base64UrlEncode(json.encode(header));
  final base64UrlPayload = base64UrlEncode(json.encode(payload));

  final data = '$base64UrlHeader.$base64UrlPayload';

  final hmac = Hmac(sha256, utf8.encode(secret));
  final signature = hmac.convert(utf8.encode(data)).bytes;

  final base64UrlSignature = base64UrlEncodeBytes(signature);

  return base64UrlSignature;
}