toXmlV2String method

  1. @visibleForTesting
String toXmlV2String()

Generates a .keyx file as described for Keepass keyfile: https://keepass.info/help/base/keys.html#keyfiles

Implementation

@visibleForTesting
String toXmlV2String() {
  final hash =
      (crypto.sha256.convert(_keyFileValue.binaryValue).bytes as Uint8List)
          .sublist(0, 4);
  final hashHexString = hexFormatLikeKeepass(convert.hex.encode(hash));
  final keyHexString =
      hexFormatLikeKeepass(convert.hex.encode(_keyFileValue.binaryValue));

  final builder = xml.XmlBuilder()
    ..processing('xml', 'version="1.0" encoding="utf-8"');
  builder.element(_nodeKeyFile, nest: () {
    builder.element(_nodeMeta, nest: () {
      builder.element(_nodeVersion, nest: () {
        builder.text('2.0');
      });
    });
    builder.element(_nodeKey, nest: () {
      builder.element(_nodeData, nest: () {
        builder.attribute(_nodeHash, hashHexString);
        builder.text(keyHexString);
      });
    });
  });
  return builder.buildDocument().toXmlString(pretty: true);
}