是否有一种模式可以在带有 node.js 的猫鼬中使用 typeof, Integer { value: 340895965n } 存储此对象
Is there a schema to store this object with typeof, Integer { value: 340895965n } in mongoose with node.js
我是 nodejs 的新手。一段时间以来,我一直在使用 paillier-js 模块,该模块 returns 这种性质的对象 typeof():Integer { value: 340895965n }。是否有一个模式可以允许用 mongoose 适当地存储这个对象。我曾尝试将对象直接传递给 mongodb 这是成功的但是当我使用 MongoDB 罗盘查看它时,我得到空的 { }
const paillier = require('paillier-js');
const bigInt = require('big-integer');
const { publicKey, privateKey } = paillier.generateRandomKeys(16);
//Set the values for the public key for the encryption
publicKey.n.value = 55687n
publicKey._n2.value = 3101041969n;
publicKey.g.value = 1696460192n;
//Set the values for the privateKey for the decryption
privateKey.lambda.value = 27608n;
privateKey.mu.value = 11489n;
privateKey._p.value = 239n;
privateKey._q.value = 233n;
privateKey.publicKey = publicKey
let num1 = 101;
let num2 = 104;
let num3 = 1;
let bn1 = bigInt(num1).mod(publicKey.n);
while (bn1.lt(0)) bn1 = bn1.add(publicKey.n);
let bn2 = bigInt(num2).mod(publicKey.n);
while (bn2.lt(0)) bn2 = bn2.add(publicKey.n);
let bn3 = bigInt(num3).mod(publicKey.n);
while (bn3.lt(0)) bn3 = bn3.add(publicKey.n);
let c1 = publicKey.encrypt(bn1);
console.log("C1: "+ c1);
console.log(c1, "is of type ", typeof(c1))
//c1 = c1.value
let c2 = publicKey.encrypt(bn2);
console.log("C2: ",c2);
//c2 = c2.value
let c3 = publicKey.encrypt(bn3);
console.log("C3: "+c3);
//c3 = c3.value
console.log(typeof(c1))
let encryptedSum = publicKey.addition(c1, c2, c3);
let decryptedSum = privateKey.decrypt(encryptedSum);
console.log('Decrypted addition:', decryptedSum.toString());
不熟悉 MongoDB,但您可以在 BigInt 上使用 toString()
,在存储到 MongoDB 之前将其转换为字符串,然后当然是从 MongoDB,您需要重新申请 BigInt
。即,
x = 14142352423459034590345934959032n;
// convert x to string...
xs = x.toString();
// convert string to BigInt
bix = BigInt(xs);
希望这对您有所帮助...
这是我所做的。
我在存储在 mongodb 中的 class 对象上使用了 toString()
。然后,当我检索它时,我在字符串上使用 BigInt()
,然后创建 class 对象的原型,然后将其值设置为 BigInt()
值。它奏效了...
//c1 is the class object
rc1 = c1.toString()
// then rc1 is stored as String in mongodb
rc1 = BigInt(rc1) //rc1 is retrieved from db and converted to BigInt
oc1 = Object.create(c2) //An Object prototype was created from c2 (an object of the class)
oc1.value = rc1; //value of the Object prototype was then set to the result of BigInt()
我是 nodejs 的新手。一段时间以来,我一直在使用 paillier-js 模块,该模块 returns 这种性质的对象 typeof():Integer { value: 340895965n }。是否有一个模式可以允许用 mongoose 适当地存储这个对象。我曾尝试将对象直接传递给 mongodb 这是成功的但是当我使用 MongoDB 罗盘查看它时,我得到空的 { }
const paillier = require('paillier-js');
const bigInt = require('big-integer');
const { publicKey, privateKey } = paillier.generateRandomKeys(16);
//Set the values for the public key for the encryption
publicKey.n.value = 55687n
publicKey._n2.value = 3101041969n;
publicKey.g.value = 1696460192n;
//Set the values for the privateKey for the decryption
privateKey.lambda.value = 27608n;
privateKey.mu.value = 11489n;
privateKey._p.value = 239n;
privateKey._q.value = 233n;
privateKey.publicKey = publicKey
let num1 = 101;
let num2 = 104;
let num3 = 1;
let bn1 = bigInt(num1).mod(publicKey.n);
while (bn1.lt(0)) bn1 = bn1.add(publicKey.n);
let bn2 = bigInt(num2).mod(publicKey.n);
while (bn2.lt(0)) bn2 = bn2.add(publicKey.n);
let bn3 = bigInt(num3).mod(publicKey.n);
while (bn3.lt(0)) bn3 = bn3.add(publicKey.n);
let c1 = publicKey.encrypt(bn1);
console.log("C1: "+ c1);
console.log(c1, "is of type ", typeof(c1))
//c1 = c1.value
let c2 = publicKey.encrypt(bn2);
console.log("C2: ",c2);
//c2 = c2.value
let c3 = publicKey.encrypt(bn3);
console.log("C3: "+c3);
//c3 = c3.value
console.log(typeof(c1))
let encryptedSum = publicKey.addition(c1, c2, c3);
let decryptedSum = privateKey.decrypt(encryptedSum);
console.log('Decrypted addition:', decryptedSum.toString());
不熟悉 MongoDB,但您可以在 BigInt 上使用 toString()
,在存储到 MongoDB 之前将其转换为字符串,然后当然是从 MongoDB,您需要重新申请 BigInt
。即,
x = 14142352423459034590345934959032n;
// convert x to string...
xs = x.toString();
// convert string to BigInt
bix = BigInt(xs);
希望这对您有所帮助...
这是我所做的。
我在存储在 mongodb 中的 class 对象上使用了 toString()
。然后,当我检索它时,我在字符串上使用 BigInt()
,然后创建 class 对象的原型,然后将其值设置为 BigInt()
值。它奏效了...
//c1 is the class object
rc1 = c1.toString()
// then rc1 is stored as String in mongodb
rc1 = BigInt(rc1) //rc1 is retrieved from db and converted to BigInt
oc1 = Object.create(c2) //An Object prototype was created from c2 (an object of the class)
oc1.value = rc1; //value of the Object prototype was then set to the result of BigInt()