add comments

This commit is contained in:
Mylloon 2022-10-28 19:23:41 +02:00
parent 765aa5357e
commit 84b2047008
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 52 additions and 0 deletions

View file

@ -42,6 +42,13 @@ const fetchFile = (list, element = undefined) => {
}
};
/**
* Update an element
* @param element Element to update
* @param text Content of the tag
* @param tag Optional: specify a new tag
* @returns new element
*/
const update = (element, text, tag = undefined) => {
if (element) {
let parent = element.parentElement;

View file

@ -1,5 +1,10 @@
import { modInv, modPow } from "./libs/bigint-mod.js";
/**
* Generate a new RSA keypair
* @param bits Key size
* @returns RSA keypair
*/
export const gen_RSA_keypair = async (bits) => {
const getPrime = (bits) => {
return new Promise((ok, ko) => {
@ -31,14 +36,32 @@ export const gen_RSA_keypair = async (bits) => {
];
};
/**
* RSA algorithm
* @param msg data
* @param key RSA key used
* @returns Encrypted data
*/
const RSA = (msg, key) => {
return modPow(msg, key[0], key[1]);
};
/**
* Encrypt a string with RSA
* @param data string
* @param key RSA key used
* @returns Int
*/
const RSA_enc = (data, key) => {
return RSA(str_to_int(data)[0], key);
};
/**
* Encrypt a big string with RSA
* @param data big string
* @param key RSA key used
* @returns Int array
*/
export const RSA_enc_data = (data, key) => {
let encoded = [];
data.match(/(.{1,100})/g).forEach((piece) => {
@ -48,10 +71,22 @@ export const RSA_enc_data = (data, key) => {
return encoded;
};
/**
* Decrypt a int to a string with RSA
* @param data int
* @param key RSA key used
* @returns string
*/
const RSA_dec = (data, key) => {
return int_to_str([RSA(data, key)]);
};
/**
* Decrypt a int array to a string with RSA
* @param data int array
* @param key RSA key used
* @returns big string
*/
export const RSA_dec_data = (data, key) => {
let decoded = "";
data.forEach((piece) => {
@ -61,6 +96,11 @@ export const RSA_dec_data = (data, key) => {
return decoded;
};
/**
* Convert a string to an int (ASCII)
* @param msg string
* @returns int (ASCII)
*/
const str_to_int = (msg) => {
let result = "";
let table = [];
@ -75,6 +115,11 @@ const str_to_int = (msg) => {
return table;
};
/**
* Convert a int to string (ASCII)
* @param msg int (ASCII)
* @returns string
*/
const int_to_str = (msg) => {
let result = "";
for (let k = 0; k < msg.length; k++) {