Skip to content

Commit

Permalink
small details changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSM100 committed Oct 25, 2024
1 parent 290584b commit 482883b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 79 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"type": "module",
"scripts": {
"serve": "vite serve --port 3000",
"build": "vite build",
Expand Down
4 changes: 2 additions & 2 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
</head>

<body>
<div id="headerDiv" style="text-align: center;">
<button id="sendRequestBtn">Send chat request</button>
<div id="sendRequestDiv">
<button id="sendRequestBtn">Send chat request.</button>
</div>

<div id="chatsDiv"></div>
Expand Down
56 changes: 28 additions & 28 deletions src/js/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,39 @@ const rsaAlgorithm = {
}

export function randomId() {
if (!crypto.randomUUID) {
const randarray = new Uint8Array(16)
crypto.getRandomValues(randarray)
if (!crypto.randomUUID) {
const randarray = new Uint8Array(16)
crypto.getRandomValues(randarray)

let hexvalues = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
let newUuidString = "";
let hexvalues = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
let newUuidString = "";

for (let i = 0; i < 16; i++) {
let num = randarray[i]
let highNibble = Math.floor(num / 16)
let lowNibble = num % 16
for (let i = 0; i < 16; i++) {
let num = randarray[i]
let highNibble = Math.floor(num / 16)
let lowNibble = num % 16

if (i === 6) {
newUuidString += "4"
} else if (i === 8) {
highNibble = highNibble & 0b0011
highNibble = highNibble | 0b1000
newUuidString += hexvalues[highNibble]
} else {
newUuidString += hexvalues[highNibble]
}
if (i === 6) {
newUuidString += "4"
} else if (i === 8) {
highNibble = highNibble & 0b0011
highNibble = highNibble | 0b1000
newUuidString += hexvalues[highNibble]
} else {
newUuidString += hexvalues[highNibble]
}

newUuidString += hexvalues[lowNibble]
newUuidString += hexvalues[lowNibble]

if (i === 3 || i === 5 || i === 7 || i === 9) {
newUuidString += "-"
}
}

return newUuidString;
} else {
return crypto.randomUUID();
if (i === 3 || i === 5 || i === 7 || i === 9) {
newUuidString += "-"
}
}

return newUuidString;
} else {
return crypto.randomUUID();
}
}


Expand Down Expand Up @@ -79,7 +79,7 @@ export async function importKey(jsonWebKey) {

/**
*
* @param {ArrayBuffer} data
* @param {BufferSource} data
* @param {CryptoKey} key
* @returns {Promise<ArrayBuffer>}
*/
Expand Down
36 changes: 15 additions & 21 deletions src/js/main.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { getSelfId, getSelfName, getSelfColor, getSelfKeys } from "./self.js";
import { exportKey, encrypt, decrypt } from "./crypto.js";
import { newChat, newMsg } from "./ui.js";
import {
$,
textToArrayBuffer,
arrayBufferToText,
arrayBufferToBase64,
base64ToArrayBuffer,
} from "./util.js";
import { $ } from "./util.js";
import { encode as arrayBufferToBase64, decode as base64ToArrayBuffer } from "base64-arraybuffer";

let users = {};

Expand Down Expand Up @@ -74,7 +69,7 @@ async function sendRequest() {
const publicKey = (await selfKeys).publicKey;
const jsonWebKey = await exportKey(publicKey);

const descr = selfName + " is requesting a private chat";
const descr = selfName + " is requesting a private chat.";
const update = {
payload: {
type: "request",
Expand All @@ -89,7 +84,7 @@ async function sendRequest() {
};

window.webxdc.sendUpdate(update, descr);
$("headerDiv").innerHTML =
$("sendRequestDiv").innerHTML =
"<small>Request sent, wait for others to respond.</small>";
}

Expand Down Expand Up @@ -138,10 +133,9 @@ export async function sendMsg(userId, userKey, text) {
*/
async function encryptText(text, key) {
try {
const arrayBuffer = textToArrayBuffer(text);
const encryptedArrayBuffer = await encrypt(arrayBuffer, key);
const base64 = arrayBufferToBase64(encryptedArrayBuffer);

const uint8Array = new TextEncoder().encode(text);
const encrypted = await encrypt(uint8Array, key);
const base64 = arrayBufferToBase64(encrypted);
return base64;
} catch (err) {
console.log(err);
Expand All @@ -151,18 +145,18 @@ async function encryptText(text, key) {

/**
*
* @param {string} encryptedText
* @param {string} base64
* @param {CryptoKey} key
* @returns {string}
*/
async function decryptText(encryptedText, key) {
async function decryptText(base64, key) {
try {
const encryptedArrayBuffer = base64ToArrayBuffer(encryptedText);
const decryptedArrayBuffer = await decrypt(encryptedArrayBuffer, key);
const text = arrayBufferToText(decryptedArrayBuffer);
const encrypted = base64ToArrayBuffer(base64);
const arrayBuffer = await decrypt(encrypted, key);
const text = new TextDecoder().decode(arrayBuffer);
return text;
} catch (err){
console.log(err)
return "This message could not be decrypted";
} catch (err) {
console.log(err);
return `This message could not be decrypted, error:\n${err}`;
}
}
12 changes: 11 additions & 1 deletion src/js/ui.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { $new, autoResize, copy } from "./util.js";
import { $new, copy } from "./util.js";
import { importKey } from "./crypto.js";
import { sendMsg } from "./main.js";

Expand Down Expand Up @@ -81,3 +81,13 @@ export function newMsg(text) {

return msg;
}


/**
*
* @param {HTMLTextAreaElement} element
*/
export function autoResize(textarea) {
textarea.style.height = "auto";
textarea.style.height = (textarea.scrollHeight - 10) + "px";
}
26 changes: 1 addition & 25 deletions src/js/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { encode, decode } from "base64-arraybuffer";
/**
*
* @param {string} id
Expand Down Expand Up @@ -27,20 +26,6 @@ export const textToArrayBuffer = (text) => new TextEncoder().encode(text);
*/
export const arrayBufferToText = (arrayBuffer) => new TextDecoder().decode(arrayBuffer);

/**
*
* @param {ArrayBuffer} arrayBuffer
* @returns {string}
*/
export const arrayBufferToBase64 = (arrayBuffer) => encode(arrayBuffer);

/**
*
* @param {string} base64
* @returns {ArrayBuffer}
*/
export const base64ToArrayBuffer = (base64) => decode(base64);

/**
*
* @param {string} text
Expand All @@ -52,13 +37,4 @@ export function copy(text) {
textarea.select();
document.execCommand("copy");
document.body.removeChild(textarea);
}

/**
*
* @param {HTMLTextAreaElement} element
*/
export function autoResize(element) {
element.style.height = "auto";
element.style.height = (element.scrollHeight - 10) + "px";
}
}
5 changes: 3 additions & 2 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ img {
vertical-align: middle;
}

#headerDiv {
#sendRequestDiv {
margin: 5px;
text-align: center;
}

.chat-div {
Expand Down Expand Up @@ -78,4 +79,4 @@ img {
max-width: 80%;
word-wrap: break-word;
cursor: copy;
}
}
1 change: 1 addition & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default defineConfig({
root: './src',
build: {
outDir: '../dist',
emptyOutDir: true,
assetsInlineLimit: 0,
reportCompressedSize: false,
},
Expand Down

0 comments on commit 482883b

Please sign in to comment.