otpus
Version:
General purpose cipher for Node & browser
141 lines (112 loc) • 3.58 kB
HTML
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.code {
height: 90vh;
width: 45vw;
font-family: Verdana, Geneva, Tahoma, sans-serif;
line-height: 1.5rem;
}
button {
padding: 15px;
font-size: 1.5em;
}
</style>
<title>OTPUS example</title>
</head>
<body>
<div><button id="evalBtn">EVAL</button></div>
<textarea class='code' id="code" cols="30" rows="10" spellcheck="false">
/*
* pure sync type otpus encryption
*/
let msg = 'this is top secret message';
let key = 'this is a secret key';
let encoded = otpus.encryptMessage( msg, key , 10); // 2 ** 10 => 1024 times
prn('base64-encoded', encoded)
let decoded = otpus.decryptMessage( encoded , key )
prn('decoded', decoded)
/*
* async type otpus encryption using Web Crypto API
*/
const Buffer = otpus.Buffer
const MBP = otpus.MBP
const encrypt = otpus.encrypt
const decrypt = otpus.decrypt
console.log( 'MBP.MB', MBP.MB('bf',32,0 ))
// case1. string data
const strData = 'hello world'
encrypt(strData, key)
.then(secretPack => {
prn('random secretPack size', secretPack.byteLength)
return decrypt(secretPack, key)
})
.then(data => {
prn('return data type is type of originData', typeof data ) // string
prn('decoded string message: ', data)
})
// case2. binary data
const binaryData = Uint8Array.from([1, 2, 3, 4])
encrypt(binaryData, key)
.then(secretPack => {
prn('random secretPack size', secretPack.byteLength)
return decrypt(secretPack, key)
})
.then(data => {
prn('instanceof Uint8Array:', data instanceof Uint8Array ) //true
prn('decoded binary data: ', MBP.hex( data ))
})
</textarea>
<textarea class='code' id="result" cols="30" rows="10" spellcheck="false">
result:</textarea>
</body>
<script type="module">
import * as otpus from '../dist/otpus.esm.js'
const encrypt = otpus.encrypt
const decrypt = otpus.decrypt
const Buffer = otpus.Buffer
// ESM async await style example.
// encryption string data.
const plainText = 'this is a secret message.'
const encPack = await encrypt(plainText, 'passPhrase', 10000)
const decodeMessage = await decrypt(encPack, 'passPhrase')
console.log('ESM: decrypted:', decodeMessage)
// encryption binary 10MBytes
const plainData = Buffer.alloc(10 * 2 ** 20)
const encData = await encrypt(plainData, 'passPhrase', 10000)
const decData = await decrypt(encData, 'passPhrase')
const some = decData.slice(0, 16)
console.log('ESM: decrypted: total byteLength:', decData.byteLength)
console.log('ESM: decrypted: show some:', some)
// error catch
try {
const secretPack = await encrypt('plain text', 'key')
const decoded = await decrypt(secretPack, 'key')
console.log('try catch. no error. decoded:', decoded)
} catch (error) {
console.log(error)
}
evalBtn.addEventListener('click', e => {
// console.log(code.value)
result.innerHTML = ''
eval(code.value)
})
function prn(tag, v) {
result.innerHTML += "\n# " + tag + ": " + v
console.log(v)
}
function prnObj(tag, v) {
v = JSON.stringify(v, null, 2)
result.innerHTML += "\n# " + tag + ": " + v
console.log(v)
}
</script>
<script src="../dist/otpus.min.js">
// this example include otpus library two times.(esm and iife) for demo.
// you can access otpus reference inside the console of devtool.
</script>
</html>