Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persisting cipher object #53

Open
hermanchitra opened this issue Sep 7, 2022 · 0 comments
Open

Persisting cipher object #53

hermanchitra opened this issue Sep 7, 2022 · 0 comments

Comments

@hermanchitra
Copy link

Is there an easier way to persist the cipher object (DKeyRatchet.AsymetricRatchet) ?
I tried serializing with toJson() and hydrating with fromJson(), but there seems to be a bug in the implementation for cipher generated from PreKeyBundleProtocol as there are some fields that is not properly hydrated.

import * as DKeyRatchet from "..";
import {Convert} from "pvtsutils";
import {Crypto} from "@peculiar/webcrypto";
import * as _ from 'lodash';


function getObjectDiff(obj1: any, obj2:any) {
    const diff = Object.keys(obj1).reduce((result, key) => {
        if (!obj2.hasOwnProperty(key)) {
            result.push(key);
        } else if (_.isEqual(obj1[key], obj2[key])) {
            const resultKeyIndex = result.indexOf(key);
            result.splice(resultKeyIndex, 1);
        }
        return result;
    }, Object.keys(obj2));
    return diff;
}

async function compareCiphers(cipher: DKeyRatchet.AsymmetricRatchet) {
    const cipherJson = await cipher.toJSON();
    const identity = cipher.identity;
    const remoteIdentity = cipher.remoteIdentity;
    const restoredCipher = await DKeyRatchet.AsymmetricRatchet.fromJSON(identity, remoteIdentity, cipherJson);
    console.log(_.isEqual(cipher, restoredCipher));
    console.log(getObjectDiff(cipher, restoredCipher));
    console.log('cipher: ', cipher.currentStep);
    console.log('restoredCipher: ', restoredCipher.currentStep);
}

async function main() {
    const crypto = new Crypto();
    DKeyRatchet.setEngine("@peculiar/webcrypto", crypto);

    const time = new Date().getTime();
    const aliceID = await DKeyRatchet.Identity.create(time, 1);
    const alicePreKeyBundle = new DKeyRatchet.PreKeyBundleProtocol();
    await alicePreKeyBundle.identity.fill(aliceID);
    alicePreKeyBundle.registrationId = aliceID.id;
    const preKey = aliceID.signedPreKeys[0];
    alicePreKeyBundle.preKeySigned.id = 0;
    alicePreKeyBundle.preKeySigned.key = preKey.publicKey;
    await alicePreKeyBundle.preKeySigned.sign(aliceID.signingKey.privateKey);

    // Bob sending encrypted message to Alice
    const bobID = await DKeyRatchet.Identity.create(2000, 1);
    const aliceBundle = await DKeyRatchet.PreKeyBundleProtocol.importProto(alicePreKeyBundle);
    const bobCipher = await DKeyRatchet.AsymmetricRatchet.create(bobID, aliceBundle);
    const bobEncryptedMessage = await bobCipher.encrypt(Convert.FromUtf8String('Hello Alice from Bob'));
    console.log('bobCipher');
    await compareCiphers(bobCipher);
}

main().catch((e) => console.error(e));

The result:

bobCipher
false
[ 'currentStep', 'remotePreKeyId', 'remotePreKeySignedId', 'id' ]
cipher: DHRatchetStep {
remoteRatchetKey: ECPublicKey {
key: CryptoKey {
algorithm: [Object],
type: 'public',
extractable: true,
usages: []
},
serialized: ArrayBuffer {
[Uint8Contents]: <76 bf 7e 3f fd 9c 2b 26 5a b9 0b 9e 02 e0 7a 3e e3 aa 36 e7 5a 48 39 f4 bc ea 82 53 7f a8 f6 52 13 03 1b
23 03 b6 e5 d8 b4 25 ed 8f 2f e6 88 c7 93 0d 97 00 3b e4 af 85 51 fe b4 a5 65 ce 47 3f>,
byteLength: 64
},
id: 'd1e3669b7d3b03567e57171c50435049424996f9b5d5c5a30bcd92bdc8a9c01f'
},
sendingChain: SendingRatchet {
counter: 1,
rootKey: CryptoKey {
algorithm: [Object],
type: 'secret',
extractable: false,
usages: [Array]
}
}
}
restoredCipher: DHRatchetStep {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant