2022-05-23 13:12:03 +00:00
|
|
|
import { AsnConvert, AsnSerializer } from "@peculiar/asn1-schema";
|
|
|
|
import * as assert from "assert";
|
|
|
|
import { Convert } from "pvtsutils";
|
2022-05-23 21:09:47 +00:00
|
|
|
import * as asn1 from "../src/schema/asn1";
|
2022-05-23 13:12:03 +00:00
|
|
|
|
|
|
|
context("ED", () => {
|
|
|
|
|
|
|
|
context("asn", () => {
|
|
|
|
|
|
|
|
it("spki - jwk", () => {
|
|
|
|
const pem = "MCowBQYDK2VwAyEAGb9ECWmEzf6FQbrBZ9w7lshQhqowtrbLDFw4rXAxZuE=";
|
|
|
|
|
2022-05-23 21:09:47 +00:00
|
|
|
const keyInfo = AsnConvert.parse(Convert.FromBase64(pem), asn1.PublicKeyInfo);
|
|
|
|
const key = new asn1.EdPublicKey(keyInfo.publicKey);
|
2022-05-23 13:12:03 +00:00
|
|
|
const jwk = key.toJSON();
|
|
|
|
|
2022-05-23 21:09:47 +00:00
|
|
|
const key2 = new asn1.EdPublicKey();
|
2022-05-23 13:12:03 +00:00
|
|
|
key2.fromJSON(jwk);
|
|
|
|
assert.strictEqual(
|
|
|
|
Convert.ToBase64(AsnSerializer.serialize(key2)),
|
|
|
|
Convert.ToBase64(AsnSerializer.serialize(key)),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
context("pkcs8 -jwk", () => {
|
|
|
|
|
|
|
|
it("without public key", () => {
|
|
|
|
const pem = "MC4CAQAwBQYDK2VwBCIEINTuctv5E1hK1bbY8fdp+K06/nwoy/HU++CXqI9EdVhC";
|
|
|
|
|
2022-05-23 21:09:47 +00:00
|
|
|
const keyInfo = AsnConvert.parse(Convert.FromBase64(pem), asn1.OneAsymmetricKey);
|
2022-05-23 13:12:03 +00:00
|
|
|
assert.strictEqual(keyInfo.publicKey, undefined);
|
2022-05-23 21:09:47 +00:00
|
|
|
const key = AsnConvert.parse(keyInfo.privateKey, asn1.EdPrivateKey);
|
2022-05-23 13:12:03 +00:00
|
|
|
const jwk = key.toJSON();
|
|
|
|
|
2022-05-23 21:09:47 +00:00
|
|
|
const key2 = new asn1.EdPrivateKey();
|
2022-05-23 13:12:03 +00:00
|
|
|
key2.fromJSON(jwk);
|
|
|
|
assert.strictEqual(
|
|
|
|
Convert.ToBase64(AsnSerializer.serialize(key2)),
|
|
|
|
Convert.ToBase64(AsnSerializer.serialize(key)),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("with public key", () => {
|
|
|
|
const pem = "MHICAQEwBQYDK2VwBCIEINTuctv5E1hK1bbY8fdp+K06/nwoy/HU++CXqI9EdVhCoB8wHQYKKoZIhvcNAQkJFDEPDA1DdXJkbGUgQ2hhaXJzgSEAGb9ECWmEzf6FQbrBZ9w7lshQhqowtrbLDFw4rXAxZuE=";
|
|
|
|
|
2022-05-23 21:09:47 +00:00
|
|
|
const keyInfo = AsnConvert.parse(Convert.FromBase64(pem), asn1.OneAsymmetricKey);
|
2022-05-23 13:12:03 +00:00
|
|
|
assert.ok(keyInfo.publicKey);
|
2022-05-23 21:09:47 +00:00
|
|
|
const key = AsnConvert.parse(keyInfo.privateKey, asn1.EdPrivateKey);
|
2022-05-23 13:12:03 +00:00
|
|
|
const jwk = key.toJSON();
|
|
|
|
|
2022-05-23 21:09:47 +00:00
|
|
|
const key2 = new asn1.EdPrivateKey();
|
2022-05-23 13:12:03 +00:00
|
|
|
key2.fromJSON(jwk);
|
|
|
|
assert.strictEqual(
|
|
|
|
Convert.ToBase64(AsnSerializer.serialize(key2)),
|
|
|
|
Convert.ToBase64(AsnSerializer.serialize(key)),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|