lay groundwork for simple dm encryption
This commit is contained in:
parent
cda662f270
commit
f8ba9fd95b
28
js/dms.js
28
js/dms.js
@ -39,6 +39,21 @@ socket.addEventListener("message", async function (event) {
|
||||
var posting_id = undefined;
|
||||
var cd = true //inversed "cooldown"
|
||||
|
||||
let encryption_keys = ""
|
||||
|
||||
function set_keys(s_key) {
|
||||
let key = extend(s_key,512)
|
||||
let msgkey = key.substring(0,128)
|
||||
let sigkey = key.substring(129,512)
|
||||
|
||||
let packed = pack_keys({
|
||||
signkey: sigkey,
|
||||
messagekey: msgkey
|
||||
})
|
||||
|
||||
encryption_keys = packed
|
||||
}
|
||||
|
||||
async function postMessage() {
|
||||
let len = document.getElementById("post-text").value.length
|
||||
if(len >= 1001) {
|
||||
@ -46,12 +61,21 @@ async function postMessage() {
|
||||
return
|
||||
}
|
||||
if(cd && posting_id!=undefined) {
|
||||
let r = await post("/api/dms/post",{"message":document.getElementById("post-text").value,"reply_id":reply_id,"receiver":currentChannel,"pid": posting_id})
|
||||
cd = false
|
||||
|
||||
let text = document.getElementById("post-text").value
|
||||
|
||||
if(typeof encrypt == "function" && encryption_keys != "") {
|
||||
text = encrypt(test,{
|
||||
packed: encryption_keys
|
||||
})
|
||||
}
|
||||
|
||||
let r = await post("/api/dms/post",{"message":text,"reply_id":reply_id,"receiver":currentChannel,"pid": posting_id})
|
||||
update_pid()
|
||||
if(window.location.href.split("?mention=")[1])location.replace('/posts');
|
||||
document.getElementById("post-text").value=""
|
||||
unreply()
|
||||
cd = false
|
||||
setTimeout(function(){
|
||||
cd = true
|
||||
},200)
|
||||
|
36
js/extend_key.js
Normal file
36
js/extend_key.js
Normal file
@ -0,0 +1,36 @@
|
||||
var sha256=function a(b){function c(a,b){return a>>>b|a<<32-b}for(var d,e,f=Math.pow,g=f(2,32),h="length",i="",j=[],k=8*b[h],l=a.h=a.h||[],m=a.k=a.k||[],n=m[h],o={},p=2;64>n;p++)if(!o[p]){for(d=0;313>d;d+=p)o[d]=p;l[n]=f(p,.5)*g|0,m[n++]=f(p,1/3)*g|0}for(b+="\x80";b[h]%64-56;)b+="\x00";for(d=0;d<b[h];d++){if(e=b.charCodeAt(d),e>>8)return;j[d>>2]|=e<<(3-d)%4*8}for(j[j[h]]=k/g|0,j[j[h]]=k,e=0;e<j[h];){var q=j.slice(e,e+=16),r=l;for(l=l.slice(0,8),d=0;64>d;d++){var s=q[d-15],t=q[d-2],u=l[0],v=l[4],w=l[7]+(c(v,6)^c(v,11)^c(v,25))+(v&l[5]^~v&l[6])+m[d]+(q[d]=16>d?q[d]:q[d-16]+(c(s,7)^c(s,18)^s>>>3)+q[d-7]+(c(t,17)^c(t,19)^t>>>10)|0),x=(c(u,2)^c(u,13)^c(u,22))+(u&l[1]^u&l[2]^l[1]&l[2]);l=[w+x|0].concat(l),l[4]=l[4]+w|0}for(d=0;8>d;d++)l[d]=l[d]+r[d]|0}for(d=0;8>d;d++)for(e=3;e+1;e--){var y=l[d]>>8*e&255;i+=(16>y?0:"")+y.toString(16)}return i};
|
||||
|
||||
function hash(str,salt,num) {
|
||||
if(!num && num!==0)num=1;
|
||||
if(!str)return;
|
||||
if(!salt)salt=""
|
||||
let ret = str;
|
||||
for (let i = 0; i < num; i++) {
|
||||
ret = sha256(ret+salt)
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
function extend(key,len) {
|
||||
let temp = []
|
||||
let out = []
|
||||
|
||||
len = len || 265
|
||||
|
||||
let hashes = 0
|
||||
|
||||
for(let i=0;i<len/64;i++) {
|
||||
|
||||
temp[0] = hash(key,"",++hashes)
|
||||
|
||||
temp[1] = hash(key,"",++hashes)
|
||||
|
||||
temp[2] = hash(key,"",++hashes)
|
||||
|
||||
out[out.length] = hash(temp[0]+temp[1],temp[2],++hashes)
|
||||
temp = []
|
||||
|
||||
}
|
||||
|
||||
return out.join("").substring(0,len)
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
const xor = require("../../../extra_modules/xor.js")
|
||||
const unsafeencrypt = require("unsafe_encrypt")
|
||||
|
||||
module.exports = {
|
||||
"setup": function(router,con,server) {
|
||||
@ -41,6 +41,11 @@ module.exports = {
|
||||
});
|
||||
})
|
||||
|
||||
router.get("/api/dms/encrypt.js", async function(req,res) {
|
||||
res.set("Access-Control-Allow-Origin","*")
|
||||
res.send(unsafeencrypt.web_version())
|
||||
})
|
||||
|
||||
//
|
||||
router.get("/api/dms/getDM", async function(req,res) {
|
||||
res.set("Access-Control-Allow-Origin","*")
|
||||
|
@ -9,6 +9,8 @@
|
||||
<script type="text/javascript" src="/js/htmlescape.js"></script>
|
||||
<script type="text/javascript" src="/js/addnavbar.js"></script>
|
||||
<script type="text/javascript" src="/js/markdown.js"></script>
|
||||
<script type="text/javascript" src="/js/extend_key.js"></script>
|
||||
<script type="text/javascript" src="/api/dms/encrypt.js"></script>
|
||||
<script src="/js/warn_message.js" charset="utf-8"></script>
|
||||
</head>
|
||||
<body>
|
||||
|
Loading…
x
Reference in New Issue
Block a user