Straightforward C14N¶
Performs the C14N transformation of a straightforward XML document.
See Notes
Documentation¶
A Python interface to SC14N <https://www.cryptosys.net/sc14n/>.
-
class
sc14n.
C14n
¶ Perform C14N transformation of XML document.
-
static
file2digest
(xmlfile, nameorid='', tranopt=0, digalg=0, tranmethod=0, exclparams='', advopts=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.
tranmethod (TranMethod) -- Transformation method.
exclparams (str) -- InclusiveNamespaces PrefixList parameter for exclusive c14n.
advopts (AdvOptions) -- Advanced option flags.
- Returns
Message digest in base64-encoded string.
- Return type
str
-
static
file2file
(outfile, xmlfile, nameorid='', tranopt=0, tranmethod=0, exclparams='', advopts=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.
tranmethod (TranMethod) -- Transformation method.
exclparams (str) -- InclusiveNamespaces PrefixList parameter for exclusive c14n.
advopts (AdvOptions) -- Advanced option flags.
- 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.OMITBYTAG
orTran.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]
whereN=1,2,3,...
With options
Tran.OMITBYID
orTran.SUBSETBYID
,nameorid
specifies the element's Id.The default Id attribute name is
Id
, so the argumentmyvalue
will find the element with attributeId="myvalue"
.To use a different attribute name - for example
ID
- write in the formID=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.OMITBYTAG) 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.OMITBYID) True
-
static
file2string
(xmlfile, nameorid='', tranopt=0, tranmethod=0, exclparams='', advopts=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.
tranmethod (TranMethod) -- Transformation method.
exclparams (str) -- InclusiveNamespaces PrefixList parameter for exclusive c14n.
advopts (AdvOptions) -- Advanced option flags.
- Returns
UTF-8-encoded string.
- Return type
str
-
static
string2digest
(xmldata, nameorid='', tranopt=0, digalg=0, tranmethod=0, exclparams='', advopts=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.
tranmethod (TranMethod) -- Transformation method.
exclparams (str) -- InclusiveNamespaces PrefixList parameter for exclusive c14n.
advopts (AdvOptions) -- Advanced option flags.
- Returns
Message digest in base64-encoded string.
- Return type
str
-
static
string2string
(xmldata, nameorid='', tranopt=0, tranmethod=0, exclparams='', advopts=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.
tranmethod (TranMethod) -- Transformation method.
exclparams (str) -- InclusiveNamespaces PrefixList parameter for exclusive c14n.
advopts (AdvOptions) -- Advanced option flags.
- Returns
UTF-8-encoded string.
- Return type
str
-
static
-
class
sc14n.
Tran
¶ Transformation options.
See also: remarks for
C14n.file2file()
.-
ENTIRE
= 0¶ Transform the entire document.
-
OMITBYID
= 17¶ Omit (exclude) the element with the given Id.
-
OMITBYTAG
= 1¶ Omit (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.
TranMethod
¶ Transformation methods.
-
EXCLUSIVE
= 256¶ Exclusive c14n without comments from RFC 3741.
-
EXCLUSIVE_WITHCOMMENTS
= 2304¶ Exclusive C14N with comments from RFC 3741.
-
INCLUSIVE
= 0¶ Inclusive c14n without comments from RFC 3076 (default).
-
INCLUSIVE_WITHCOMMENTS
= 2048¶ Inclusive C14N with comments from RFC 3076.
-
-
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.
AdvOptions
¶ Advanced option flags.
-
DEFAULT
= 0¶ Use default options.
-
FLATTEN
= 65536¶ Flatten the XML - remove all ignorable whitespace between tags.
-
-
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
orWin64
.
-
static
licence_type
()¶ Return licence type:
D
= DeveloperT
= 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.
-
static
-
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.
-
static
-
exception
sc14n.
Error
(value)¶ Raised when a call to a core library function returns an error, or some obviously wrong parameter is detected.
Notes¶
The following C14N transformations are supported:
"inclusive canonicalization" from Canonical XML Version 1.0 (reproduced as RFC 3076) with identifiers
http://www.w3.org/TR/2001/REC-xml-c14n-20010315
http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments
"exclusive canonicalization" from Exclusive XML Canonical Version 1.0 (reproduced as RFC 3741) with identifiers
http://www.w3.org/2001/10/xml-exc-c14n#
http://www.w3.org/2001/10/xml-exc-c14n#WithComments
Input is expected to be a well-formed XML document.
Supported XML encodings are
UTF-8
,ISO-8859-1
andUS-ASCII
.With Python 3, input in a "string" is expected to be a standard Python 3 UTF-8 string (str type). Output in a "string" is always a UTF-8-encoded str type.