@libp2p/crypto
Version:
Crypto primitives for libp2p
65 lines • 1.68 kB
JavaScript
import { base58btc } from 'multiformats/bases/base58';
import { CID } from 'multiformats/cid';
import {} from 'multiformats/hashes/digest';
import { equals as uint8ArrayEquals } from 'uint8arrays/equals';
import { hashAndSign, utils, hashAndVerify } from './index.js';
export class RSAPublicKey {
type = 'RSA';
_key;
_raw;
_multihash;
constructor(key, digest) {
this._key = key;
this._multihash = digest;
}
get raw() {
if (this._raw == null) {
this._raw = utils.jwkToPkix(this._key);
}
return this._raw;
}
toMultihash() {
return this._multihash;
}
toCID() {
return CID.createV1(114, this._multihash);
}
toString() {
return base58btc.encode(this.toMultihash().bytes).substring(1);
}
equals(key) {
if (key == null || !(key.raw instanceof Uint8Array)) {
return false;
}
return uint8ArrayEquals(this.raw, key.raw);
}
verify(data, sig) {
return hashAndVerify(this._key, sig, data);
}
}
export class RSAPrivateKey {
type = 'RSA';
_key;
_raw;
publicKey;
constructor(key, publicKey) {
this._key = key;
this.publicKey = publicKey;
}
get raw() {
if (this._raw == null) {
this._raw = utils.jwkToPkcs1(this._key);
}
return this._raw;
}
equals(key) {
if (key == null || !(key.raw instanceof Uint8Array)) {
return false;
}
return uint8ArrayEquals(this.raw, key.raw);
}
sign(message) {
return hashAndSign(this._key, message);
}
}
//# sourceMappingURL=rsa.js.map