decodeEd25519PrivateKeyPkcs8 static method
Implementation
static SecretBytes decodeEd25519PrivateKeyPkcs8(List<int> der) {
final root = DerReader(der).readNode();
_expectTag(root, DerTag.sequence, 'PrivateKeyInfo SEQUENCE');
final fields = root.children();
if (fields.length < 3) {
throw CoreErrors.cryptoFailed(
"decodeEd25519PrivateKeyPkcs8",
reason: 'Malformed PrivateKeyInfo',
);
}
_expectTag(fields[0], DerTag.integer, 'version');
final algId = fields[1];
_expectTag(algId, DerTag.sequence, 'AlgorithmIdentifier');
final algChildren = algId.children();
if (algChildren.isEmpty) {
throw CoreErrors.cryptoFailed(
"decodeEd25519PrivateKeyPkcs8",
reason: 'Malformed AlgorithmIdentifier',
);
}
final algOid = DerUtils.decodeOid(algChildren[0].value);
if (algOid != Oids.ed25519) {
throw CoreErrors.cryptoFailed(
"decodeEd25519PrivateKeyPkcs8",
reason: 'Not an Ed25519 key (algorithm OID $algOid)',
);
}
_expectTag(fields[2], DerTag.octetString, 'privateKey');
final innerNode = DerReader(fields[2].value).readNode();
_expectTag(innerNode, DerTag.octetString, 'CurvePrivateKey');
if (innerNode.value.length != 32) {
throw CoreErrors.cryptoFailed(
"decodeEd25519PrivateKeyPkcs8",
reason: 'Ed25519 seed must be 32 bytes',
);
}
return SecretBytes.take(innerNode.value);
}