new_keys static method

Future<SubtleCryptoECDSAP256Keys> new_keys({
  1. bool extractable = false,
})

This function generates a new key-pair using the SubtleCrypto generateKey method.

Implementation

static Future<SubtleCryptoECDSAP256Keys> new_keys({bool extractable = false}) async {
    if (Crypto.supported == false || window.crypto!.subtle == null ) {
        throw UnsupportedError('Subtle Crypto api not supported on this browser');
    }

    Object crypto_key_pair = await promiseToFuture(callMethod(window.crypto!.subtle!, 'generateKey', [
        EcKeyGenParams(
            name: 'ECDSA',
            namedCurve: 'P-256'
        ),
        extractable,
        jsify(['sign'])
    ]));

    CryptoKey private_key = getProperty(crypto_key_pair, 'privateKey');
    CryptoKey public_key  = getProperty(crypto_key_pair, 'publicKey');

    // 'spki' format is with the clude of the DER bytes
    ByteBuffer public_key_DER_native_byte_buffer = await promiseToFuture(callMethod(window.crypto!.subtle!, 'exportKey', ['spki', public_key]));

    return SubtleCryptoECDSAP256Keys._(
        public_key_DER: public_key_DER_native_byte_buffer.asUint8List(),
        public_key: public_key,
        private_key: private_key
    );

}