├── LICENSE └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenPGP.js-examples 2 | A place to share your OpenPGP.js examples with comments to help build this into the largest library of its kind. 3 | 4 | As developers, we know how frustrating it is to research complex algorithms. Often, you have to filter through many sites to find what you need. Well, we'd like to fix that. I'm starting off this library with some routines that I use here at fastdata2go.com to get things started. 5 | 6 | Please feel free to add to this library. I only ask that you follow these guidlines: 7 | - Follow this format. 8 | - Be current with the latest OpenPGP.js release. 9 | 10 | ####Generate Keypair 11 | ```javascript 12 | /** 13 | * Generate a Private and Public keypair 14 | * @param {numBits} Integer - Any multiple of 1024. 2048 is recommended. 15 | * @param {userid} String - should be like: Alice Mayfield 16 | * @param {passphrase} String - password should be a 4-5 word sentence (20+ chars) 17 | * @return {key} String - Encrypted ASCII armored keypair (contains both Private and Public keys) 18 | */ 19 | function keygen(numBits, userId, passphrase) { 20 | var openpgp = window.openpgp; 21 | var key = openpgp.generateKeyPair({ 22 | numBits: numBits, 23 | userId: userId, 24 | passphrase: passphrase 25 | }); 26 | return key; 27 | } 28 | ``` 29 | 30 | 31 | 32 | ####Encrypt Message 33 | ```javascript 34 | /** 35 | * Encrypt a message using the recipient's public key. 36 | * @param {pubkey} String - Encrypted ASCII Armored public key. 37 | * @param {message} String - Your message to the recipient. 38 | * @return {pgpMessage} String - Encrypted ASCII Armored message. 39 | */ 40 | 41 | function encrypt_message(pubkey, message) { 42 | var openpgp = window.openpgp; 43 | var key = pubkey; 44 | var publicKey = openpgp.key.readArmored(key); 45 | var pgpMessage = openpgp.encryptMessage(publicKey.keys, message); 46 | return pgpMessage; 47 | } 48 | ``` 49 | 50 | 51 | 52 | ####Decrypt Message 53 | ```javascript 54 | /** 55 | * Decrypt a message using your private key. 56 | * @param {pubkey} String - Your recipient's public key. 57 | * @param {privkey} String - Your private key. 58 | * @param {passphrase} String - Your ultra-strong password. 59 | * @param {encoded_message} String - Your message from the recipient. 60 | * @return {decrypted} String - Decrypted message. 61 | */ 62 | 63 | function decrypt_message(pubkey, privkey, passphrase, encoded_message) { 64 | var openpgp = window.openpgp; 65 | var privKeys = openpgp.key.readArmored(privkey); 66 | var publicKeys = openpgp.key.readArmored(pubkey); 67 | var privKey = privKeys.keys[0]; 68 | var success = privKey.decrypt(passphrase); 69 | var message = openpgp.message.readArmored(encoded_message); 70 | var decrypted = openpgp.decryptMessage(privKey, message); 71 | return decrypted; 72 | } 73 | ``` 74 | 75 | 76 | ####Sign Message 77 | ```javascript 78 | /** 79 | * Sign a message using your private key. 80 | * @param {pubkey} String - Your recipient's public key. 81 | * @param {privkey} String - Your private key. 82 | * @param {passphrase} String - Your ultra-strong password. 83 | * @param {message} String - Your message from the recipient. 84 | * @return {signed} String - Signed message. 85 | */ 86 | 87 | function sign_message(pubkey, privkey, passphrase, message){ 88 | var openpgp = window.openpgp; 89 | var priv = openpgp.key.readArmored(privkey); 90 | var pub = openpgp.key.readArmored(pubkey); 91 | var privKey = priv.keys[0]; 92 | var success = priv.decrypt(passphrase); 93 | var signed = openpgp.signClearMessage(priv.keys, message); 94 | return signed; 95 | } 96 | ``` 97 | 98 | 99 | ####Verify Signature 100 | ```javascript 101 | /** 102 | * Sign a message using your private key. 103 | * @param {pubkey} String - Your recipient's public key. 104 | * @param {privkey} String - Your private key. 105 | * @param {passphrase} String - Your ultra-strong password. 106 | * @param {signed_message} String - Your signed message from the recipient. 107 | * @return {signed} Boolean - True (1) is a valid signed message. 108 | */ 109 | 110 | function verify_signature(pubkey, privkey, passphrase, signed_message) { 111 | var openpgp = window.openpgp; 112 | var privKeys = openpgp.key.readArmored(privkey); 113 | var publicKeys = openpgp.key.readArmored(pubkey); 114 | var privKey = privKeys.keys[0]; 115 | var success = privKey.decrypt(passphrase); 116 | var message = openpgp.cleartext.readArmored(signed_message); 117 | var verified = openpgp.verifyClearSignedMessage(publicKeys.keys, message); 118 | if (verified.signatures[0].valid === true) { 119 | return '1'; 120 | } else { 121 | return '0'; 122 | } 123 | } 124 | ``` 125 | 126 | 127 | #### Public Key Length 128 | ```javascript 129 | /** 130 | * Determines public key size (1024, 2048...) 131 | * @param {data} String - Your recipient's Encrypted ASCII Armored public key. 132 | * @return {size} Integer - Length of the public key. 133 | */ 134 | 135 | function get_publickey_length(data) { 136 | var publicKey = openpgp.key.readArmored(data); 137 | var publicKeyPacket = publicKey.keys[0].primaryKey; 138 | if (publicKeyPacket !== null) { 139 | strength = getBitLength(publicKeyPacket); 140 | } 141 | 142 | function getBitLength(publicKeyPacket) { 143 | var size = -1; 144 | if (publicKeyPacket.mpi.length > 0) { 145 | size = (publicKeyPacket.mpi[0].byteLength() * 8); 146 | } 147 | return size; 148 | } 149 | } 150 | ``` 151 | 152 | 153 | ####HTML5 Storage 154 | ```javascript 155 | HTML5 supports offline storage in the browser. 156 | One possible use is key storage. For non-IE storage, use this format: 157 | 158 | localStorage.setItem("companyname.privkey", privkey); 159 | localStorage.setItem("companyname.pubkey", key.publicKeyArmored); 160 | 161 | localStorage.getItem("companyname.privkey"); 162 | localStorage.getItem("companyname.pubkey"); 163 | ``` 164 | 165 | 166 | 167 | --------------------------------------------------------------------------------