Last active
March 1, 2017 08:11
-
-
Save ppbntl19/9346093315097877277c2add5e019549 to your computer and use it in GitHub Desktop.
Tested on Safari/Ios/Chrome/Firefox)
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
//Jsfiddle https://jsfiddle.net/h2u4fowj/58/ | |
function str2ab(str) { | |
var buf = new ArrayBuffer(str.length); | |
var bufView = new Uint8Array(buf); | |
for (var i = 0, strLen = str.length; i < strLen; i++) { | |
bufView[i] = str.charCodeAt(i); | |
} | |
return buf; | |
} | |
//Create base64 from arraybuffer | |
function _arrayBufferToBase64(buffer) { | |
var binary = ''; | |
var bytes = new Uint8Array(buffer); | |
var len = bytes.byteLength; | |
for (var i = 0; i < len; i++) { | |
binary += String.fromCharCode(bytes[i]); | |
} | |
return window.btoa(binary); | |
} | |
//Convert base64 to unitArray | |
function _base64ToArrayBuffer(base64) { | |
var binary_string = window.atob(base64); | |
var len = binary_string.length; | |
var bytes = new Uint8Array(len); | |
for (var i = 0; i < len; i++) { | |
bytes[i] = binary_string.charCodeAt(i); | |
} | |
return bytes; | |
} | |
var keys = { | |
"_id": "58b5acc26886780d006e8353", | |
"private_key": { | |
"kty": "RSA", | |
"n": "ltN-rIU4V88KLRflYWfI94oDDWxOG6x38kXDYjnHRc0U3uyYmb0zPv7hvTF-oG7v947Ckk1uK3Pj26dNy9KxA8LumtRP2sYRKUGR_9vBPCTd3QKe_f7ILg7qw0CYSHBLhnKncKY1e0kWXjLCPbrKXJvGTyVmmt480OsZtl5nc-s", | |
"e": "AQAB", | |
"d": "hiQdwnRNkxMJeueI06PJghcV9edAZK50k13mksLOb08hY87LPdie7kUkIe8WLrvb4rHubM4-0Jimq-HVeBSv50n3fRkKGowH15oFzDpzRYq0orLiu786WARjpko1vOx6sEioCNIugdCs4OFTSEB8XmbEjuw0c-LESW7C84Ri9Qk", | |
"p": "xtbHolLT2lQNCXWlD1PmdU2D9fJsint83ngw1WMWJxxkEa7aLRQnnc3Qp55lzdwkNqSspkyJFuYfswmr6g0MZQ", | |
"q": "wi9KnhE7ecR20tY-PYL7s40S8O-z0oSEv0Y_qgk9d5RDNBrRQWt7fbeWDM97jX9EwPKUX2cWlSZaNSqmrtgyDw", | |
"dp": "rmNUtzK5hzRMMs2xNB47cwnHAH_bXiErxNnYym2a0jZ-NVwLEZopnKcTps6vLrsyL5KBsWBSo3i0uIt08VL8WQ", | |
"dq": "CjCPGtJOq6oajh-97l5kNNzPRKNfbUmU6bCXAKYxeofmKuLJXkR0m3k5v3xXwGoQQou4K3VuYu87RpTxYDVqEw", | |
"qi": "B9gwww80iD3I2XQucKW7-xHmagnQAGyYO6xHma-iNSiKLyruBhkuM7fLRMQfcMoeTwxd28NG31ju0xQXZxETWQ" | |
}, | |
"public_key": { | |
"kty": "RSA", | |
"n": "ltN-rIU4V88KLRflYWfI94oDDWxOG6x38kXDYjnHRc0U3uyYmb0zPv7hvTF-oG7v947Ckk1uK3Pj26dNy9KxA8LumtRP2sYRKUGR_9vBPCTd3QKe_f7ILg7qw0CYSHBLhnKncKY1e0kWXjLCPbrKXJvGTyVmmt480OsZtl5nc-s", | |
"e": "AQAB" | |
} | |
} | |
var private_key = keys.private_key; | |
var public_key = keys.public_key; | |
// Fix Apple prefix if needed | |
if (window.crypto && !window.crypto.subtle && window.crypto.webkitSubtle) { | |
window.crypto.subtle = window.crypto.webkitSubtle; // Won't work if subtle already exists | |
//Update private key | |
console.log("safari browser") | |
private_key = str2ab(JSON.stringify(private_key)); | |
public_key = str2ab(JSON.stringify(public_key)); | |
} | |
//global variable | |
var signature_hash; | |
//****************Sign*********************// | |
var imported_private_key = ''; | |
window.crypto.subtle.importKey( | |
"jwk", //can be "jwk" (public or private), "spki" (public only), or "pkcs8" (private only) | |
private_key, { //these are the algorithm options | |
name: "RSASSA-PKCS1-v1_5", | |
hash: { | |
name: "SHA-256" | |
}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512" | |
}, | |
false, //whether the key is extractable (i.e. can be used in exportKey) | |
["sign"] //"verify" for public key import, "sign" for private key imports | |
) | |
.then(function(private_key) { | |
console.log("private_key", private_key); | |
imported_private_key = private_key; | |
}) | |
.catch(function(err) { | |
console.log("Something went wrong while importing the key: " + err.message + "\n" + err.stack); | |
//Record Error | |
}); | |
var plaintext_buffer = str2ab("pooranprakash"); | |
setTimeout(function() { | |
window.crypto.subtle.sign({ | |
"name": "RSASSA-PKCS1-v1_5", | |
"modulusLength": 1024, | |
"hash": { | |
"name": "SHA-256" | |
} | |
}, | |
imported_private_key, //from generateKey or importKey above | |
plaintext_buffer //ArrayBuffer of data you want to sign | |
).then(function(signature) { | |
console.log("signature", signature); | |
//Save hash | |
signature_hash = _arrayBufferToBase64(signature) | |
}) | |
.catch(function(err) { | |
console.log("Something went wrong while signing the signature: " + err.message + "\n" + err.stack); | |
//Record Error | |
}) | |
}, 5000); | |
//**************************Verify****************************// | |
window.crypto.subtle.importKey( | |
"jwk", //can be "jwk" (public or private), "spki" (public only), or "pkcs8" (private only) | |
public_key, { //these are the algorithm options | |
name: "RSASSA-PKCS1-v1_5", | |
hash: { | |
name: "SHA-256" | |
}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512" | |
}, | |
false, //whether the key is extractable (i.e. can be used in exportKey) | |
["verify"] //"verify" for public key import, "sign" for private key imports | |
) | |
.then(function(public_key) { | |
//verify file | |
imported_public_key = public_key; | |
}) | |
.catch(function(err) { | |
console.error(err); | |
}); | |
setTimeout(function() { | |
window.crypto.subtle.verify({ | |
"name": "RSASSA-PKCS1-v1_5", | |
"modulusLength": 1024, | |
"hash": { | |
"name": "SHA-256" | |
} | |
}, | |
imported_public_key, | |
_base64ToArrayBuffer(signature_hash), | |
plaintext_buffer | |
).then(function(successful) { | |
// Returns either a Blob containing the original plaintext (if verification was successful) or null (if not). | |
if (successful) { | |
console.log("Signature is valid."); | |
return successful | |
} else { | |
console.log("Signature is invalid."); | |
return null; | |
} | |
}) | |
.catch(function(err) { | |
console.error("Something went wrong verifying: " + err.message + "\n" + err.stack); | |
}); | |
}, 10000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment