Skip to content
This repository has been archived by the owner on Aug 5, 2022. It is now read-only.

Add toJSON and fromJSON methods #30

Closed
tracker1 opened this issue Aug 10, 2017 · 0 comments
Closed

Add toJSON and fromJSON methods #30

tracker1 opened this issue Aug 10, 2017 · 0 comments

Comments

@tracker1
Copy link

tracker1 commented Aug 10, 2017

re: #3 #28

// global deserializer method
RSAKey.fromJSON = function(key) {
    let json = JSON.parse(key);
    if (json.type !== 'RSAKey') return null;
    let rsa = new RSAKey();
    rsa.setPrivateEx(json.n, json.e, json.d, json.p, json.q, json.dmp1, json.dmq1, json.coeff);
    return rsa;
}

// instance serializer, JSON.stringify(object_with_RSAKey) will serialize as expected.
RSAKey.prototype.toJSON = function() {
    return JSON.stringify({
      type: 'RSAKey',
      coeff: this.coeff.toString(16),
      d: this.d.toString(16),
      dmp1: this.dmp1.toString(16),
      dmq1: this.dmq1.toString(16),
      e: this.e.toString(16),
      n: this.n.toString(16),
      p: this.p.toString(16),
      q: this.q.toString(16)
    })
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant