unit tests
This commit is contained in:
@ -2,7 +2,7 @@
|
||||
Convert an ArrayBuffer into a string
|
||||
from https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
|
||||
*/
|
||||
function ab2str(buf) {
|
||||
export function ab2str(buf) {
|
||||
return String.fromCharCode.apply(null, new Uint8Array(buf));
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ function ab2str(buf) {
|
||||
Convert a string into an ArrayBuffer
|
||||
from https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
|
||||
*/
|
||||
function str2ab(str) {
|
||||
export function str2ab(str) {
|
||||
const buf = new ArrayBuffer(str.length);
|
||||
const bufView = new Uint8Array(buf);
|
||||
for (let i = 0, strLen = str.length; i < strLen; i++) {
|
||||
@ -19,13 +19,13 @@ function str2ab(str) {
|
||||
return buf;
|
||||
}
|
||||
|
||||
function exportedKeyToPem(key, type) {
|
||||
export function exportedKeyToPem(key, type) {
|
||||
let exportedAsString = ab2str(key);
|
||||
let exportedAsBase64 = window.btoa(exportedAsString);
|
||||
return `-----BEGIN ${type.toUpperCase()} KEY-----\n${exportedAsBase64}\n-----END ${type.toUpperCase()} KEY-----`;
|
||||
}
|
||||
|
||||
function pemToKey(pemKey, type) {
|
||||
export function pemToKey(pemKey, type) {
|
||||
const base64 = pemKey.replace(`-----BEGIN ${type.toUpperCase()} KEY-----`, '').replace(`-----END ${type.toUpperCase()} KEY-----`, '').trim();
|
||||
const binaryDerString = window.atob(base64);
|
||||
const binaryDer = str2ab(binaryDerString);
|
||||
@ -41,7 +41,7 @@ function pemToKey(pemKey, type) {
|
||||
}
|
||||
|
||||
|
||||
async function genKey() {
|
||||
export async function genKey() {
|
||||
// Generate keys
|
||||
const { publicKey, privateKey } = await crypto.subtle.generateKey(
|
||||
{
|
||||
|
@ -7,8 +7,8 @@
|
||||
<link rel="stylesheet" href="/style.css">
|
||||
<script src="/socket.io/socket.io.js" defer></script>
|
||||
<script src="/script.js" defer></script>
|
||||
<script src="/ecc.js" defer></script>
|
||||
<script src="/popups.js" defer></script>
|
||||
<script type="module" src="/ecc.js" defer></script>
|
||||
<script type="module" src="/popups.js" defer></script>
|
||||
<!--load bootstrap-->
|
||||
<link rel="stylesheet" href="/css/bootstrap.min.css" />
|
||||
</head>
|
||||
|
@ -1,4 +1,6 @@
|
||||
const currentUrl = window.location.href;
|
||||
import { ab2str, exportedKeyToPem, pemToKey, genKey } from "./ecc.js";
|
||||
|
||||
|
||||
// close popups with escape key
|
||||
document.addEventListener("keydown", (event) => {
|
||||
@ -78,10 +80,10 @@ document.getElementById("loginconfirm").addEventListener("click", async function
|
||||
const { challenge } = await response.json();
|
||||
console.log("Received challenge:", challenge);
|
||||
|
||||
privKey = await pemToKey(inputFieldPrivateKey.value, "private");
|
||||
let privKey = await pemToKey(inputFieldPrivateKey.value, "private");
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
encodedData = encoder.encode(challenge);
|
||||
let encodedData = encoder.encode(challenge);
|
||||
|
||||
// Sign the data using the private key.
|
||||
const signature = await crypto.subtle.sign(
|
||||
|
Reference in New Issue
Block a user