getToken static method

BearerTokenResultDTO getToken(
  1. String data,
  2. DateTime live,
  3. String secretKey
)

Ключ должен быть длиной: 16 symbols/bytes - 128 bits 24 symbols/bytes - 192 bits 32 symbols/bytes - 256 bits

Implementation

static BearerTokenResultDTO getToken(String data, DateTime live, String secretKey) {
  if(!(secretKey.length == 16 || secretKey.length == 24 || secretKey.length == 32)) return const BearerTokenResultDTO(status: BearerTokenStatus.brokenSecretKey);

  final String header = _ecnrypt(jsonEncode({
    "alg":"HS256","type":"JWT"
  }), secretKey);

  final String payload = _ecnrypt(jsonEncode({
    "data": data,
    "live": live.toIso8601String()
  }), secretKey);

  final String signature = _ecnrypt(base64Url.encode(Hmac(sha256, utf8.encode(secretKey)).convert("$header.$payload".codeUnits).bytes), secretKey);

  return BearerTokenResultDTO(result: "$header.$payload.$signature", status: BearerTokenStatus.done);
}