SSHPem.decode constructor
SSHPem.decode(
- String pem
Implementation
factory SSHPem.decode(String pem) {
final lines = pem.trim().split("\n");
final header = lines.first;
final footer = lines.last;
if (!header.startsWith(_pemHeaderBegin)) {
throw FormatException('PEM header must start with $_pemHeaderBegin');
}
if (!footer.startsWith(_pemFooterBegin)) {
throw FormatException('PEM footer must start with $_pemFooterBegin');
}
if (!header.endsWith(_pemEnd)) {
throw FormatException('PEM header must end with $_pemEnd');
}
if (!footer.endsWith(_pemEnd)) {
throw FormatException('PEM footer must end with $_pemEnd');
}
final type = header.substring(
_pemHeaderBegin.length,
header.length - _pemEnd.length,
);
final footerType = footer.substring(
_pemFooterBegin.length,
footer.length - _pemEnd.length,
);
if (type != footerType) {
throw FormatException('Type mismatch: $type != $footerType');
}
final pemHeader = <String, String>{};
final pemBodyLines = <String>[];
for (final line in lines.sublist(1, lines.length - 1)) {
if (line.trim().isEmpty) {
continue;
}
if (line.contains(':')) {
if (pemBodyLines.isNotEmpty) {
throw FormatException('Unexpected header line: $line');
} else {
final parts = line.split(':');
pemHeader[parts[0].trim()] = parts[1].trim();
continue;
}
}
pemBodyLines.add(line);
}
final pemBody = pemBodyLines.join('');
final content = base64.decode(pemBody);
return SSHPem(type, pemHeader, content);
}