added json to ab helper op

This commit is contained in:
Simon Arnell 2025-01-29 21:28:13 +02:00
parent 08d66388a7
commit 0ebafdc7f2
3 changed files with 56 additions and 13 deletions

View file

@ -0,0 +1,44 @@
/**
* @author Configured Things Ltd. [getconfigured@configuredthings.com]
* @copyright Crown Copyright 2025
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
/**
* JSON to ArrayBuffer operation
*/
class JSONToArrayBuffer extends Operation {
/**
* JSONToArrayBuffer constructor
*/
constructor() {
super();
this.name = "JSON to ArrayBuffer";
this.module = "Default";
this.description = "Serialises a JSON object to an ArrayBuffer";
this.infoURL = ""; // Usually a Wikipedia link. Remember to remove localisation (i.e. https://wikipedia.org/etc rather than https://en.wikipedia.org/etc)
this.inputType = "JSON";
this.outputType = "ArrayBuffer";
this.args = [
];
}
/**
* @param {JSON} input
* @param {Object[]} args
* @returns {ArrayBuffer}
*/
run(input, args) {
const messageStr = JSON.stringify(input);
const textEncoder = new TextEncoder();
const messageAb = textEncoder.encode(messageStr).buffer;
return messageAb;
}
}
export default JSONToArrayBuffer;

View file

@ -23,7 +23,7 @@ class LibHydrogenCurve25519Signing extends Operation {
this.module = "Crypto";
this.description = "Computes a signature for a message using the lightweight LibHydrogen cryptography library";
this.infoURL = "https://libhydrogen.org/";
this.inputType = "JSON";
this.inputType = "ArrayBuffer";
this.outputType = "JSON";
this.args = [
{
@ -41,7 +41,7 @@ class LibHydrogenCurve25519Signing extends Operation {
/* eslint-disable camelcase */
/**
* @param {JSON} input
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {JSON}
*/
@ -240,8 +240,8 @@ function reserve(offset, length) {
*/
/**
* Digital signing of a JSON object
* @param {JSON} input - A JSON object to be signed
* Digital signing of an ArrayBuffer's contents
* @param {ArrayBuffer} input - An ArrayBuffer to be signed
* @param {string} context - A string used to define the context of the signing operation,
* see {@link https://github.com/jedisct1/libhydrogen/wiki/Contexts}
* @param {Uint8Array} privateKey - The private key to use for the digital signing operation
@ -264,12 +264,11 @@ async function sign(input, context, privateKey) {
contextArr.set([contextAb.at(i)], i);
}
const messageStr = JSON.stringify(input);
const messageAb = textEncoder.encode(messageStr);
const messageArr = reserve(offset, messageAb.length);
const messageTypedArr = new Uint8Array(input);
const messageArr = reserve(offset, messageTypedArr.length);
for (let i = 0; i < messageAb.length; i++) {
messageArr.set([messageAb.at(i)], i);
for (let i = 0; i < input.length; i++) {
messageArr.set([messageTypedArr.at(i)], i);
}
// Generate a key pair

View file

@ -10,15 +10,15 @@ import OperationError from "../errors/OperationError.mjs";
/**
* Configured Things - Digital Security by Design - Signed JSON to Text operation
*/
class ConfiguredThingsDigitalSecurityByDesignSignedJSONToText extends Operation {
class SignedJSONToArrayBuffer extends Operation {
/**
* ConfiguredThingsDigitalSecurityByDesignSignedJSONToText constructor
* SignedJSONToArrayBuffer constructor
*/
constructor() {
super();
this.name = "Configured Things - Digital Security by Design - LibHydrogen Signed JSON to Text";
this.name = "Signed JSON to ArrayBuffer";
this.module = "Default";
this.description = "Converts signed JSON to text";
this.infoURL = ""; // Usually a Wikipedia link. Remember to remove localisation (i.e. https://wikipedia.org/etc rather than https://en.wikipedia.org/etc)
@ -49,4 +49,4 @@ class ConfiguredThingsDigitalSecurityByDesignSignedJSONToText extends Operation
}
}
export default ConfiguredThingsDigitalSecurityByDesignSignedJSONToText;
export default SignedJSONToArrayBuffer;