-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa083a6
commit 6065df3
Showing
17 changed files
with
466 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,4 @@ | |
/coverage/ | ||
/.vscode | ||
/test/test*.js | ||
xmldsig.js | ||
npm-debug.log |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/*.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<title>XmlDSig</title> | ||
<style> | ||
textarea { | ||
width: 100%; | ||
} | ||
</style> | ||
<script src="src/helper.js"></script> | ||
<script src="src/main.js"></script> | ||
<script src="src/promise.min.js"></script> | ||
<script src="src/webcrypto-liner.shim.js"></script> | ||
<script src="src/asmcrypto.min.js"></script> | ||
<script src="src/elliptic.min.js"></script> | ||
<script src="../../dist/xmldsig.js"></script> | ||
</head> | ||
|
||
<body> | ||
<h2>XmlDSig::Sign</h2> | ||
<div> | ||
<div> | ||
<h3>Signing params</h3> | ||
<h4>Select key algorithm</h4> | ||
<select id="key"> | ||
<option value="rsassa" selected>RSASSA-PKCS1-v1_5</option> | ||
<option value="rsapss">RSA-PSS</option> | ||
<option value="ecdsa">ECDSA</option> | ||
</select> | ||
<br/> | ||
<lable for="keyValue">Iclude key vallue:</lable> | ||
<input id="keyValue" type="checkbox" checked> | ||
<h4>Select digest algorithm</h4> | ||
<select id="digest"> | ||
<option value="SHA-1">SHA-1</option> | ||
<option value="SHA-256" selected>SHA-256</option> | ||
<option value="SHA-384">SHA-384</option> | ||
<option value="SHA-512">SHA-512</option> | ||
</select> | ||
<h4>Type of signature</h4> | ||
<lable for="enveloped">Enveloped:</lable> | ||
<input id="enveloped" type="checkbox" checked> | ||
<h4>Select canon method</h4> | ||
<select id="canon"> | ||
<option value="c14n" selected>c14n</option> | ||
<option value="c14n-com">c14n with comment</option> | ||
<option value="exc-c14n" selected>exc-c14n</option> | ||
<option value="exc-c14n-com">exc-c14n with comment</option> | ||
</select> | ||
<h4>Set xml</h4> | ||
<textarea id="xml" cols="30" rows="10"> | ||
</textarea> | ||
|
||
<input type="button" value="Sign" onclick="sign()"> | ||
</div> | ||
<div id="result"> | ||
<h3>Result</h3> | ||
<textarea id="jwk" cols="30" rows="3" placeholder="JWK" readonly></textarea> | ||
<textarea id="signature" cols="30" rows="10" placeholder="XML Signature" readonly></textarea> | ||
</div> | ||
</div> | ||
<script> | ||
document.getElementById("xml").value = "<root><child>Simple xml</child></root>" | ||
</script> | ||
</body> | ||
|
||
</html> |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
var Browser = { | ||
IE: "Internet Explorer", | ||
Safari: "Safari", | ||
Edge: "Edge", | ||
Chrome: "Chrome", | ||
Firefox: "Firefox Mozilla", | ||
}; | ||
/** | ||
* Returns info about browser | ||
*/ | ||
function BrowserInfo() { | ||
var res = { | ||
name: "", | ||
version: "" | ||
}; | ||
var userAgent = self.navigator.userAgent; | ||
var reg; | ||
if (reg = /edge\/([\d\.]+)/i.exec(userAgent)) { | ||
res.name = Browser.Edge; | ||
res.version = reg[1]; | ||
} | ||
else if (/msie/i.test(userAgent)) { | ||
res.name = Browser.IE; | ||
res.version = /msie ([\d\.]+)/i.exec(userAgent)[1]; | ||
} | ||
else if (/Trident/i.test(userAgent)) { | ||
res.name = Browser.IE; | ||
res.version = /rv:([\d\.]+)/i.exec(userAgent)[1]; | ||
} | ||
else if (/chrome/i.test(userAgent)) { | ||
res.name = Browser.Chrome; | ||
res.version = /chrome\/([\d\.]+)/i.exec(userAgent)[1]; | ||
} | ||
else if (/safari/i.test(userAgent)) { | ||
res.name = Browser.Safari; | ||
res.version = /([\d\.]+) safari/i.exec(userAgent)[1]; | ||
} | ||
else if (/firefox/i.test(userAgent)) { | ||
res.name = Browser.Firefox; | ||
res.version = /firefox\/([\d\.]+)/i.exec(userAgent)[1]; | ||
} | ||
return res; | ||
} | ||
|
||
stringToBuffer = function (text) { | ||
var res = new Uint8Array(text.length); | ||
for (var i = 0; i < text.length; i++) | ||
res[i] = text.charCodeAt(i); | ||
return res; | ||
}; | ||
buffer2string = function (buffer) { | ||
var res = ""; | ||
for (var i = 0; i < buffer.length; i++) | ||
res += String.fromCharCode(buffer[i]); | ||
return res; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
function getAlgorithm() { | ||
var $key = document.getElementById("key"); | ||
|
||
var alg = {}; | ||
switch ($key.value) { | ||
case "rsassa": | ||
alg = { | ||
name: "RSASSA-PKCS1-v1_5", | ||
hash: "SHA-256", | ||
modulusLength: 1024, | ||
publicExponent: new Uint8Array([1, 0, 1]), | ||
}; | ||
break; | ||
case "rsapss": | ||
alg = { | ||
name: "RSA-PSS", | ||
hash: "SHA-256", | ||
modulusLength: 1024, | ||
publicExponent: new Uint8Array([1, 0, 1]), | ||
saltLength: 32, | ||
}; | ||
break; | ||
case "ecdsa": | ||
alg = { | ||
name: "ECDSA", | ||
hash: "SHA-256", | ||
namedCurve: "P-256", | ||
}; | ||
break; | ||
} | ||
return alg; | ||
} | ||
|
||
function getHashAlgorithm() { | ||
return document.getElementById("digest").value; | ||
} | ||
|
||
function getCanonMethod() { | ||
return document.getElementById("canon").value; | ||
} | ||
|
||
function isEnveloped() { | ||
return document.getElementById("enveloped").checked; | ||
} | ||
|
||
function useKeyValue() { | ||
return document.getElementById("keyValue").checked; | ||
} | ||
|
||
function getXml() { | ||
return document.getElementById("xml").value; | ||
} | ||
|
||
function generateKey(alg) { | ||
return crypto.subtle.generateKey(alg, false, ["sign", "verify"]) | ||
} | ||
|
||
function exportKey(key) { | ||
return crypto.subtle.exportKey("jwk", key) | ||
} | ||
|
||
function error(e) { | ||
alert(e.message); | ||
console.error(e); | ||
} | ||
|
||
function sign() { | ||
var transforms = []; | ||
if (isEnveloped()) | ||
transforms.push("enveloped"); | ||
transforms.push(getCanonMethod()); | ||
console.log(transforms); | ||
|
||
var alg = getAlgorithm(); | ||
var keys, signature, res = {}; | ||
Promise.resolve() | ||
.then(function () { | ||
return generateKey(alg); | ||
}) | ||
.then(function (ks) { | ||
keys = ks; | ||
return exportKey(ks.publicKey) | ||
}) | ||
.then(function (jwk) { | ||
res.jwk = jwk; | ||
}) | ||
.then(function () { | ||
signature = new XmlDSigJs.SignedXml(); | ||
|
||
return signature.Sign( // Signing document | ||
alg, // algorithm | ||
keys.privateKey, // key | ||
XmlDSigJs.XmlSignatureObject.Parse(getXml()),// document | ||
{ // options | ||
keyValue: useKeyValue() ? keys.publicKey : void 0, | ||
references: [ | ||
{ hash: getHashAlgorithm(), transforms: transforms } | ||
] | ||
}); | ||
}) | ||
.then(function () { | ||
var sig = signature.toString() | ||
res.signature = sig; | ||
|
||
document.getElementById("jwk").value = JSON.stringify(res.jwk); | ||
document.getElementById("signature").value = res.signature; | ||
}) | ||
.catch(function (e) { | ||
console.error(e); | ||
}); | ||
|
||
} | ||
|
||
function verify() { | ||
var $xml = document.getElementById("xml"); | ||
var $info = document.getElementById("signature_info"); | ||
if (!$xml.value) | ||
return error(new Error("Unable to get XML")); | ||
var xml = XmlDSigJs.XmlSignatureObject.Parse($xml.value); | ||
var signature = new XmlDSigJs.SignedXml(); | ||
var xmlSignatures = XmlDSigJs.Select(xml, "//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']"); | ||
|
||
if (!(xmlSignatures && xmlSignatures.length)) | ||
return error("Cannot get XML signature from XML document"); | ||
|
||
signature.LoadXml(xmlSignatures[0]); | ||
|
||
signature.Verify() | ||
.then(function (res) { | ||
var info = []; | ||
info.push("Signature valid: " + res.toString()); | ||
$info.textContent = info.join("\n"); | ||
}) | ||
.catch(function (e) { | ||
error(e); | ||
}); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.