signRsaPkcs1 function

Future<List<int>> signRsaPkcs1 (
  1. String privateKey,
  2. String message,
  3. {HashName digest: HashName.sha256}
)

Implementation

Future<List<int>> signRsaPkcs1(String privateKey, String message,
    {HashName digest = HashName.sha256}) async {
  final tempDir = await Directory.systemTemp.createTemp();
  final privateKeyPath = path.join(tempDir.path, 'privatekey.pem');
  final f =
      await File(privateKeyPath).writeAsString(privateKey, encoding: utf8);
  final messagePath = path.join(tempDir.path, 'message.txt');
  await File(messagePath).writeAsString(message, encoding: utf8);

  final res = await Process.run(
      'openssl',
      [
        'dgst',
        '-${digest.name}',
        '-sigopt',
        'rsa_padding_mode:pkcs1',
        '-sign',
        '$privateKeyPath',
        '$messagePath'
      ],
      stdoutEncoding: null,
      includeParentEnvironment: true,
      runInShell: true);

  if (res.exitCode != 0) {
    await tempDir.delete(recursive: true);
    throw OpensslException(
        res.exitCode, systemEncoding.decode(res.stdout), res.stderr);
  }

  await tempDir.delete(recursive: true);

  return res.stdout;
}