xml_crypto 3.2.0 copy "xml_crypto: ^3.2.0" to clipboard
xml_crypto: ^3.2.0 copied to clipboard

Xml digital signature library for Dart. For signing and verifying XML documents.

example/example.dart

//Copyright (C) 2022 Potix Corporation. All Rights Reserved.
//History: Tue Feb 15 18:40:31 CST 2022
// Author: rudyhuang

import 'dart:io';

import 'package:xml/xml.dart';
import 'package:xml_crypto/xml_crypto.dart';

void main() {
  final xml = "<library>"
      "<book>"
      "<name>Harry Potter</name>"
      "</book>"
      "</library>";

  //sign an xml document
  signXml(xml, "//*[local-name()='book']", 'client.pem', 'result.xml');

  print('xml signed successfully');

  final signedXml = File('result.xml').readAsStringSync();
  print('validating signature...');

  //validate an xml document
  if (validateXml(signedXml, 'client_public.pem')) {
    print('signature is valid');
  } else {
    print('signature not valid');
  }
}

void signXml(String xml, String xpath, String key, String dest) {
  final sig = SignedXml()
    ..signingKey = File(key).readAsBytesSync()
    ..addReference(xpath)
    ..computeSignature(xml);
  File(dest).writeAsStringSync(sig.signedXml);
}

bool validateXml(String xml, String key) {
  final doc = XmlDocument.parse(xml);
  final signature = doc.findAllElements('Signature').first;
  final sig = SignedXml()
    ..keyInfoProvider = FileKeyInfo(key)
    ..loadSignature(signature);
  final res = sig.checkSignature(xml);
  if (!res) print(sig.validationErrors);
  return res;
}
6
likes
150
pub points
77%
popularity

Publisher

verified publisherquire.io

Xml digital signature library for Dart. For signing and verifying XML documents.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

crypto, ninja, rsa_pkcs, xml, xpath_selector, xpath_selector_xml_parser

More

Packages that depend on xml_crypto