|
| 1 | +import { SecureCellSeal, SecureCellTokenProtect, SecureCellContextImprint } from "jsthemis" |
| 2 | + |
| 3 | +const message = Buffer.from('Test Message Please Ignore', 'utf-8') |
| 4 | +const context = Buffer.from('Secure Cell example code','utf-8') |
| 5 | +const master_key = Buffer.from('bm8sIHRoaXMgaXMgbm90IGEgdmFsaWQgbWFzdGVyIGtleQ==', 'base64') |
| 6 | +const passphrase = 'My Litte Secret: Passphrase Is Magic' |
| 7 | + |
| 8 | +console.log('# Secure Cell in Seal mode\n') |
| 9 | +console.log('## Master key API\n') |
| 10 | +const scellMK = SecureCellSeal.withKey(master_key) |
| 11 | +const encrypted_message = scellMK.encrypt(message) |
| 12 | +console.log('Encrypted: ' + Buffer.from(encrypted_message).toString('base64')) |
| 13 | +const decrypted_message = scellMK.decrypt(encrypted_message) |
| 14 | +console.log('Decrypted: ' + Buffer.from(decrypted_message).toString()) |
| 15 | +console.log() |
| 16 | + |
| 17 | +const encrypted_message2 = Buffer.from('AAEBQAwAAAAQAAAAEQAAAC0fCd2mOIxlDUORXz8+qCKuHCXcDii4bMF8OjOCOqsKEdV4+Ga2xTHPMupFvg==', 'base64') |
| 18 | +const decrypted_message2 = scellMK.decrypt(encrypted_message2) |
| 19 | +console.log('Decrypted (simulator): ' + Buffer.from(decrypted_message2).toString()) |
| 20 | +console.log() |
| 21 | +console.log('## Passphrase API\n') |
| 22 | + |
| 23 | +const scellPW = SecureCellSeal.withPassphrase(passphrase) |
| 24 | +const encrypted_message3 = scellPW.encrypt(message) |
| 25 | +console.log('Encrypted: ' + Buffer.from(encrypted_message3).toString('base64')) |
| 26 | +const decrypted_message3 = scellPW.decrypt(encrypted_message3) |
| 27 | +console.log('Decrypted: ' + Buffer.from(decrypted_message3).toString()) |
| 28 | +console.log() |
| 29 | + |
| 30 | +console.log('# Secure Cell in Token Protect mode\n') |
| 31 | + |
| 32 | +const scellTP = SecureCellTokenProtect.withKey(master_key) |
| 33 | +const encrypted_message4 = scellTP.encrypt(message) |
| 34 | +console.log('Encrypted: ' + Buffer.from(encrypted_message4.data).toString('base64')) |
| 35 | +console.log('Auth token: ' + Buffer.from(encrypted_message4.token).toString('base64')) |
| 36 | +const decrypted_message4 = scellTP.decrypt(encrypted_message4.data, encrypted_message4.token) |
| 37 | +console.log('Decrypted: ' + Buffer.from(decrypted_message4).toString()) |
| 38 | +console.log('') |
| 39 | + |
| 40 | +console.log('# Secure Cell in Context Imprint mode\n') |
| 41 | +const scellCI = SecureCellContextImprint.withKey(master_key) |
| 42 | +const encrypted_message5 = scellCI.encrypt(message, context) |
| 43 | +console.log('Encrypted: ' + Buffer.from(encrypted_message5).toString('base64')) |
| 44 | +const decrypted_message5 = scellCI.decrypt(encrypted_message5, context) |
| 45 | +console.log('Decrypted: ' + Buffer.from(decrypted_message5).toString()) |
| 46 | +console.log('') |
| 47 | +console.log('SecureCell example code finished') |
| 48 | + |
| 49 | + |
0 commit comments