Straightforward C14N

Performs the C14N transformation of a straightforward XML document.

See Notes

A Python interface to SC14N <http://www.di-mgt.com.au/sc14n/>.

class sc14n.C14n

Perform C14N transformation of XML document.

static file2digest(xmlfile, nameorid='', tranopt=0, digalg=0)

Compute digest value of C14N transformation of XML document (file-to-digest).

Parameters:
  • xmlfile (str) -- Name of input XML file.
  • nameorid (str) -- To specify the tag name or Id. See remarks for C14n.file2file().
  • tranopt (Tran) -- Transformation option.
  • digalg (DigAlg) -- Digest algorithm.
Returns:

Message digest in base64 format.

static file2file(outfile, xmlfile, nameorid='', tranopt=0)

Perform C14N transformation of XML document (file-to-file).

Parameters:
  • outfile (str) -- Name of output file to create.
  • xmlfile (str) -- Name of input XML file.
  • nameorid (str) -- To specify the tag name or Id.
  • tranopt (Tran) -- Transformation option.
Returns:

True if successful, False otherwise.

Return type:

bool

Remarks:

Use the nameorid parameter to specify the element of the XML document to include or exclude.

With options Tran.EXCLUDEBYTAG or Tran.SUBSETBYTAG, nameorid specifies the element's tag name.

  • By default, the first element with a matching tag name will be chosen.
  • To specify the Nth element, write as tagname[N] where N=1,2,3,...

With options Tran.EXCLUDEBYID or Tran.SUBSETBYID, nameorid specifies the element's Id.

  • The default Id attribute name is Id, so the argument myvalue will find the element with attribute Id="myvalue".
  • To use a different attribute name - for example ID - write in the form ID=myvalue with no quotes.

Exactly one element will be excluded or included. Tag names and Id values are case sensitive. It is an error (NO_DATA_ERROR) if no matching element is found.

Examples

>>> # Example 1. Excludes the first element with the tag name <Signature>
>>> r = C14n.file2file("c14nfile1.txt", "input.xml", "Signature", Tran.EXCLUDEBYTAG)
True
>>> # Example 2. Finds and transforms the first element with the tag name <SignedInfo>
>>> r = C14n.file2file("c14nfile2.txt", "input.xml", "SignedInfo", Tran.SUBSETBYTAG)
True
>>> # Example 3. Finds and transforms the third element with the tag name <Data>
>>> r = C14n.file2file("c14nfile3.txt", "input.xml", "Data[3]", Tran.SUBSETBYTAG)
True
>>> # Example 4. Finds and transforms the element with attribute Id="foo"
>>> r = C14n.file2file("c14nfile4.txt", "input.xml", "foo", Tran.SUBSETBYID)
True
>>> # Example 5. Finds and transforms the element with attribute ID="bar"
>>> r = C14n.file2file("c14nfile5.txt", "input.xml", "ID=bar", Tran.SUBSETBYID)
True
>>> # Example 6. Excludes element with attribute Id="thesig"
>>> r = C14n.file2file("c14nfile6.txt", "input.xml", "thesig", Tran.EXCLUDEBYID)
True
static file2string(xmlfile, nameorid='', tranopt=0)

Perform C14N transformation of XML document (file-to-string).

Parameters:
  • xmlfile (str) -- Name of input XML file.
  • nameorid (str) -- To specify the tag name or Id. See remarks for C14n.file2file().
  • tranopt (Tran) -- Transformation option.
Returns:

UTF-8 encoded data

Return type:

str

static string2digest(xmldata, nameorid='', tranopt=0, digalg=0)

Compute digest value of C14N transformation of XML document (string-to-digest).

Parameters:
  • xmldata (str) -- XML data to be processed.
  • nameorid (str) -- To specify the tag name or Id. See remarks for C14n.file2file().
  • tranopt (Tran) -- Transformation option.
  • digalg (DigAlg) -- Digest algorithm.
Returns:

Message digest in base64 format.

static string2string(xmldata, nameorid='', tranopt=0)

Perform C14N transformation of XML document (string-to-string).

Parameters:
  • xmldata (str) -- XML data to be processed.
  • nameorid (str) -- To specify the tag name or Id. See remarks for C14n.file2file().
  • tranopt (Tran) -- Transformation option.
Returns:

UTF-8 encoded data

Return type:

str

class sc14n.Tran

Transformation options.

See also: remarks for C14n.file2file().

ENTIRE = 0

Transform the entire document.

EXCLUDEBYID = 17

Exclude the element with the given Id.

EXCLUDEBYTAG = 1

Exclude the element with the given tag name.

SUBSETBYID = 18

Transform the subset with the given Id.

SUBSETBYTAG = 2

Transform the subset with the given tag name.

class sc14n.DigAlg

Message digest algorithms.

DEFAULT = 0

Use default digest algorithm.

SHA1 = 0

Use SHA-1 digest (default)

SHA256 = 8192

Use SHA-256 digest

class sc14n.Gen

General info about the core library DLL.

static compile_time()

Return date and time the core library DLL was last compiled.

static core_platform()

Return the platform of the core library DLL: Win32 or Win64.

static licence_type()

Return licence type: D=Developer T=Trial.

static module_name()

Return full path name of the current process's core library DLL.

static version()

Return the release version of the core library DLL as an integer value.

class sc14n.Err

Details of errors returned by the core library.

static error_lookup(n)

Return a description of error code n.

static last_error()

Return the last error message set by the toolkit, if any.

exception sc14n.Error(value)

Raised when a call to a core library function returns an error, or some obviously wrong parameter is detected.

Notes

  • All C14N transformations use "inclusive canonicalization" without comments from Canonical XML Version 1.0 with identifier

    http://www.w3.org/TR/2001/REC-xml-c14n-20010315

  • Input is expected to be a well-formed XML document.

  • Supported XML encodings are UTF-8, ISO-8859-1 and US-ASCII.

  • Output in a "string" is UTF-8 encoded and technically a byte array with a zero terminating character.

    • To display an output string correctly use s.decode('utf-8')