How to use @peculiar/asn1-schema - 10 common examples

To help you get started, we’ve selected a few @peculiar/asn1-schema examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github PeculiarVentures / webcrypto / src / asn / rsa_private_key.ts View on Github external
// RSAPrivateKey ::= SEQUENCE {
//   version           Version,
//   modulus           INTEGER,  -- n
//   publicExponent    INTEGER,  -- e
//   privateExponent   INTEGER,  -- d
//   prime1            INTEGER,  -- p
//   prime2            INTEGER,  -- q
//   exponent1         INTEGER,  -- d mod (p-1)
//   exponent2         INTEGER,  -- d mod (q-1)
//   coefficient       INTEGER,  -- (inverse of q) mod p
//   otherPrimeInfos   OtherPrimeInfos OPTIONAL
// }

export class RsaPrivateKey {

  @AsnProp({ type: AsnPropTypes.Integer, converter: AsnIntegerConverter })
  public version = 0;

  @AsnProp({ type: AsnPropTypes.Integer, converter: AsnIntegerArrayBufferConverter })
  @JsonProp({ name: "n", converter: JsonBase64UrlArrayBufferConverter })
  public modulus = new ArrayBuffer(0);

  @AsnProp({ type: AsnPropTypes.Integer, converter: AsnIntegerArrayBufferConverter })
  @JsonProp({ name: "e", converter: JsonBase64UrlArrayBufferConverter })
  public publicExponent = new ArrayBuffer(0);

  @AsnProp({ type: AsnPropTypes.Integer, converter: AsnIntegerArrayBufferConverter })
  @JsonProp({ name: "d", converter: JsonBase64UrlArrayBufferConverter })
  public privateExponent = new ArrayBuffer(0);

  @AsnProp({ type: AsnPropTypes.Integer, converter: AsnIntegerArrayBufferConverter })
  @JsonProp({ name: "p", converter: JsonBase64UrlArrayBufferConverter })
github PeculiarVentures / webcrypto / src / asn / rsa_public_key.ts View on Github external
// RFC 3437
// https://tools.ietf.org/html/rfc3447#appendix-A.1.1
//
// RSAPublicKey ::= SEQUENCE {
//   modulus           INTEGER,  -- n
//   publicExponent    INTEGER,  -- e
// }

export class RsaPublicKey {

  @AsnProp({ type: AsnPropTypes.Integer, converter: AsnIntegerArrayBufferConverter })
  @JsonProp({ name: "n", converter: JsonBase64UrlArrayBufferConverter })
  public modulus = new ArrayBuffer(0);

  @AsnProp({ type: AsnPropTypes.Integer, converter: AsnIntegerArrayBufferConverter })
  @JsonProp({ name: "e", converter: JsonBase64UrlArrayBufferConverter })
  public publicExponent = new ArrayBuffer(0);

}
github PeculiarVentures / webcrypto / src / asn / rsa_public_key.ts View on Github external
import { AsnProp, AsnPropTypes } from "@peculiar/asn1-schema";
import { JsonProp } from "@peculiar/json-schema";
import { AsnIntegerArrayBufferConverter, JsonBase64UrlArrayBufferConverter } from "../converters";

// RFC 3437
// https://tools.ietf.org/html/rfc3447#appendix-A.1.1
//
// RSAPublicKey ::= SEQUENCE {
//   modulus           INTEGER,  -- n
//   publicExponent    INTEGER,  -- e
// }

export class RsaPublicKey {

  @AsnProp({ type: AsnPropTypes.Integer, converter: AsnIntegerArrayBufferConverter })
  @JsonProp({ name: "n", converter: JsonBase64UrlArrayBufferConverter })
  public modulus = new ArrayBuffer(0);

  @AsnProp({ type: AsnPropTypes.Integer, converter: AsnIntegerArrayBufferConverter })
  @JsonProp({ name: "e", converter: JsonBase64UrlArrayBufferConverter })
  public publicExponent = new ArrayBuffer(0);

}
github PeculiarVentures / webcrypto / test / asn.ts View on Github external
it("serialize", () => {
        const key = JsonParser.fromJSON(json, { targetSchema: asn.RsaPublicKey });

        const keyInfo = new asn.PublicKeyInfo();
        keyInfo.publicKeyAlgorithm.algorithm = "1.2.840.113549.1.1.1";
        keyInfo.publicKeyAlgorithm.parameters = null;
        keyInfo.publicKey = AsnSerializer.serialize(key);

        const asnKeyInfo = Buffer.from(AsnSerializer.serialize(keyInfo));
        assert.equal(asnKeyInfo.equals(bytes), true);
      });
github PeculiarVentures / webcrypto / test / asn.ts View on Github external
it("serialize", () => {
        const key = JsonParser.fromJSON(json, { targetSchema: asn.RsaPrivateKey });

        const keyInfo = new asn.PrivateKeyInfo();
        keyInfo.privateKeyAlgorithm.algorithm = "1.2.840.113549.1.1.1";
        keyInfo.privateKeyAlgorithm.parameters = null;
        keyInfo.privateKey = AsnSerializer.serialize(key);

        const asnKeyInfo = Buffer.from(AsnSerializer.serialize(keyInfo));
        assert.equal(asnKeyInfo.equals(bytes), true);
      });
github PeculiarVentures / webcrypto / test / asn.ts View on Github external
it("parse", () => {
        const keyInfo = AsnParser.parse(bytes, asn.PrivateKeyInfo);
        const key = AsnParser.parse(keyInfo.privateKey, asn.RsaPrivateKey);

        const jsonKey = JsonSerializer.toJSON(key);
        assert.deepEqual(jsonKey, json);
      });
github PeculiarVentures / webcrypto-liner / src / mechs / ec / crypto.ts View on Github external
private static exportPkcs8Key(key: EcCryptoKey) {
    const keyInfo = new asn.PrivateKeyInfo();
    keyInfo.privateKeyAlgorithm.algorithm = this.ASN_ALGORITHM;
    keyInfo.privateKeyAlgorithm.parameters = AsnSerializer.serialize(
      new asn.ObjectIdentifier(getOidByNamedCurve(key.algorithm.namedCurve)),
    );
    keyInfo.privateKey = AsnSerializer.serialize(this.exportEcKey(key));

    return AsnSerializer.serialize(keyInfo);
  }
github PeculiarVentures / webcrypto / src / mechs / ec / public_key.ts View on Github external
public fromJSON(json: JsonWebKey) {
    const key = JsonParser.fromJSON(json, { targetSchema: asn.EcPublicKey });

    const keyInfo = new asn.PublicKeyInfo();
    keyInfo.publicKeyAlgorithm.algorithm = "1.2.840.10045.2.1";
    keyInfo.publicKeyAlgorithm.parameters = AsnSerializer.serialize(
      new ObjectIdentifier(getOidByNamedCurve(json.crv!)),
    );
    keyInfo.publicKey = AsnSerializer.toASN(key).valueHex;

    this.data = Buffer.from(AsnSerializer.serialize(keyInfo));

    return this;
  }
}
github PeculiarVentures / webcrypto / src / mechs / ec / crypto.ts View on Github external
protected static async importPrivateKey(asnKey: asn.EcPrivateKey, algorithm: EcKeyImportParams, extractable: boolean, keyUsages: KeyUsage[]) {
    const keyInfo = new asn.PrivateKeyInfo();
    keyInfo.privateKeyAlgorithm.algorithm = "1.2.840.10045.2.1";
    keyInfo.privateKeyAlgorithm.parameters = AsnSerializer.serialize(new ObjectIdentifier(getOidByNamedCurve(algorithm.namedCurve)));
    keyInfo.privateKey = AsnSerializer.serialize(asnKey);

    const key = new EcPrivateKey();
    key.data = Buffer.from(AsnSerializer.serialize(keyInfo));

    key.algorithm = Object.assign({}, algorithm) as EcKeyAlgorithm;
    key.extractable = extractable;
    key.usages = keyUsages;

    return key;
  }
github PeculiarVentures / webcrypto / src / mechs / rsa / crypto.ts View on Github external
protected static importPublicKey(asnKey: asn.RsaPublicKey, algorithm: RsaHashedImportParams, extractable: boolean, keyUsages: KeyUsage[]) {
    const keyInfo = new asn.PublicKeyInfo();
    keyInfo.publicKeyAlgorithm.algorithm = "1.2.840.113549.1.1.1";
    keyInfo.publicKeyAlgorithm.parameters = null;
    keyInfo.publicKey = AsnSerializer.serialize(asnKey);

    const key = new RsaPublicKey();
    key.data = Buffer.from(AsnSerializer.serialize(keyInfo));

    key.algorithm = Object.assign({}, algorithm) as RsaHashedKeyAlgorithm;
    key.algorithm.publicExponent = new Uint8Array(asnKey.publicExponent);
    key.algorithm.modulusLength = asnKey.modulus.byteLength << 3;
    key.extractable = extractable;
    key.usages = keyUsages;

    return key;
  }

@peculiar/asn1-schema

Decorators for ASN.1 schemas building

MIT
Latest version published 7 months ago

Package Health Score

71 / 100
Full package analysis