如何在dart中创建X509证书?

问题描述 投票:0回答:1

我已经生成了公钥和私钥

Uint8List generatePrivateKey(String input1, String input2) {
    // Concatenate the inputs to create the seed
    String seed = input1 + input2;

    // Create a SHA-256 digest of the seed
    Digest sha256Digest = SHA256Digest();
    Uint8List seedBytes = Uint8List.fromList(seed.codeUnits);
    Uint8List digest = sha256Digest.process(seedBytes);

    // Create an ECC private key using the secp256k1 curve
    ECCurve_secp256k1 curve = ECCurve_secp256k1();
    ECPrivateKey privateKey =
        ECPrivateKey(BigInt.parse(hex.encode(digest), radix: 16), curve);

    // Make sure the private key is within the valid range for the curve
    BigInt n = curve.n;
    BigInt privateKeyInt = privateKey.d!;
    privateKeyInt = privateKeyInt % n;

    String privateKeyHex = privateKeyInt.toRadixString(16).padLeft(64, '0');
    Uint8List privateKeyBytes = Uint8List.fromList(hex.decode(privateKeyHex));

    return privateKeyBytes;
  }

  ECPublicKey generatePublicKey(Uint8List privateKeyBytes) {
    ECCurve_secp256k1 curve = ECCurve_secp256k1();
    ECPrivateKey privateKey = ECPrivateKey(
        BigInt.parse(hex.encode(privateKeyBytes), radix: 16), curve);

    ECPoint? publicKeyPoint = curve.G * privateKey.d!;
    return ECPublicKey(publicKeyPoint, curve);
  }

这就是我使用 pointycastle 库生成密钥的方式,现在我想创建一个 X509 证书。

我尝试过X509库,dart:io,密码学库。for rg

import 'dart:io';
import 'dart:typed_data';
import 'package:asn1lib/asn1lib.dart';
import 'package:cryptography/cryptography.dart';
Future<X509Certificate> generateSelfSignedCertificate(Uint8List privateKeyBytes,final publicKey) async {
  // final publicKey = generatePublicKey(privateKeyBytes);

  final certificate = X509Certificate(
    subject: X509DistinguishedName(commonName: 'Self-Signed'),
    issuer: X509DistinguishedName(commonName: 'Self-Signed'),
   ` serialNumber: DateTime.now().millisecondsSinceEpoch,
    validity: X509Validity(
      notBefore: DateTime.now(),
      notAfter: DateTime.now().add(Duration(days: 365)),
    ),
    subjectPublicKey: await publicKey.extractRawSubjectPublicKey(),
    issuerPrivateKey: privateKeyBytes,
  );

  return certificate;
}

我在

X509DistinguishedName
上收到错误,它未定义

flutter dart cryptography x509certificate x509
1个回答
0
投票

为了生成证书,过程是:

  1. 准备一个证书签名请求 (CSR),其中包含要放入证书中的信息(主题 DN、公钥、有效期、扩展名等)。 CSR 使用与所附公钥匹配的私钥进行签名,以证明它是由密钥所有者创建的(称为拥有证明
  2. 使用私钥签署CSR以制作X509证书

当证书是自签名时,用于签署证书的私钥与证书的公钥相匹配。

如果您只想生成自签名证书,可以使用 basic_utils 包,它提供了 CryptoUtils 和

String generateSelfSignedCertificate() {
  var pair = CryptoUtils.generateEcKeyPair();
  var privKey = pair.privateKey as ECPrivateKey;
  var pubKey = pair.publicKey as ECPublicKey;
  var dn = {
    'CN': 'Self-Signed',
  };
  var csr = X509Utils.generateEccCsrPem(dn, privKey, pubKey);

  var x509PEM = X509Utils.generateSelfSignedCertificate(
    privKey,
    csr,
    365,
  );
  return x509PEM;
}

该软件包似乎没有提供通用证书签名功能,但可以通过查看

generateSelfSignedCertificate
代码

来编写它
© www.soinside.com 2019 - 2024. All rights reserved.