bitcluster
Version:
Simplified Client-Side Encryption
66 lines (58 loc) • 1.89 kB
JavaScript
class BitCluster{
static encrypt(args){
if(typeof args === "undefined" || typeof args.data === "undefined" || typeof args.key === "undefined"){
return("Password/Data cannot be null")
}else{
var Crypto = require('crypto-js')
let key = args.key
let data = args.data
let k1 = String(Crypto.SHA3(key+data))
let hash = String(Crypto.SHA3(Crypto.SHA256(data)))
let h1 = String(Crypto.AES.encrypt(k1, key))
let h2 = String(Crypto.AES.encrypt(data, k1))
let obj = {
h2: h2,
h1: h1,
hash: hash
}
return obj
}
};
static decrypt(args){
if(typeof args === "undefined" || typeof args.h2 === "undefined" || typeof args.h1 === "undefined" || typeof args.pass === "undefined" || typeof args.hash === "undefined"){
return("Password/Data cannot be null")
}else{
var Crypto = require('crypto-js')
let h2 = args.h2
let h1 = args.h1
let pass = args.pass
let hash = args.hash
var k1_bytes = Crypto.AES.decrypt(h1, pass);
let k1 = k1_bytes.toString(Crypto.enc.Utf8);
let d1_bytes = Crypto.AES.decrypt(h2, k1)
let d1 = d1_bytes.toString(Crypto.enc.Utf8);
let comp_sha = String(Crypto.SHA3(Crypto.SHA256(d1)))
if(hash === comp_sha){
return {data: d1, status: "OK"}
}else{
return {data: null, status: "Something isn't right. Check your password, h1, h2, and hash and try again."}
}
}
}
static qr_encrypt(data){
if(typeof data === "undefined" || data === ''){
return("Data cannot be null")
}
var n = require('nanoid')
var QRCode = require('qrcode')
var key = n(256)
var obj = {
data: data,
key: key
}
var res = this.encrypt(obj)
res.qr = QRCode.toDataURL(key)
return res
}
}
module.exports = BitCluster