importRawKey static method
Import an AesCtrSecretKey
from raw keyData
.
KeyData must be either:
- 16 bytes (128 bit) for AES-128, or,
- 32 bytes (256 bit) for AES-256.
Support for AES-192 (24 byte keys) is intentionally omitted, in line with the decision not support AES-192 in Chrome.
Example
import 'dart:convert' show utf8;
import 'dart:typed_data' show Uint8List;
import 'package:webcrypto/webcrypto.dart';
final rawKey = Uint8List(16);
fillRandomBytes(rawKey);
// Import key from raw bytes
final k = await AesCtrSecretKey.importRawKey(rawKey);
// Use a unique counter for each message.
final ctr = Uint8List(16); // always 16 bytes
fillRandomBytes(ctr);
// Length of the counter, the N'th right most bits of ctr are incremented
// for each block, the left most 128 - N bits are used as static nonce.
final N = 64;
// Encrypt a message
final c = await k.encryptBytes(utf8.encode('hello world'), ctr, N);
// Decrypt message (requires the same counter ctr and length N)
print(utf8.decode(await k.decryptBytes(c, ctr, N))); // hello world
Implementation
static Future<AesCtrSecretKey> importRawKey(List<int> keyData) {
return impl.aesCtr_importRawKey(keyData);
}