How to use the asn1 function from node-forge

Find comprehensive JavaScript node-forge.asn1 code examples handpicked from public code repositorys.

node-forge.asn1 is a library used for encoding and decoding ASN.1 data in JavaScript.

173
174
175
176
177
178
179
180
181
182

    return logins;
}

function decodeLoginData(b64) {
    const asn1 = forge.asn1.fromDer(forge.util.decode64(b64));
    return {
        iv: asn1.value[1].value[1].value,
        data: asn1.value[2].value
    };
fork icon63
star icon475
watch icon31

+ 7 other calls in file

106
107
108
109
110
111
112
113
114
115
        capture: 'macDigest'
    }]
}, {
    name: 'PFX.macData.macSalt',
    tagClass: forge.asn1.Class.UNIVERSAL,
    type: forge.asn1.Type.OCTETSTRING,
    constructed: false,
    capture: 'macSalt'
}, {
    name: 'PFX.macData.iterations',
fork icon62
star icon142
watch icon52

+ 191 other calls in file

How does node-forge.asn1 work?

ASN.1 is a standard interface that defines rules for the encoding of data structures and is used in node-forge to specify and encode data types, such as integers, bit strings, and sequences, in a way that can be read and interpreted by different systems. The node-forge.asn1 module provides a set of functions and classes to create, read, and manipulate ASN.1 data structures, including ASN.1 BER encoding and decoding, DER encoding and decoding, and more.

0
1
2
3
4
5
6
7
8
9
const inherits = require('util').inherits;
const type = require('../core').type;
const events = require('events');
const crypto = require('crypto');
const forge = require('node-forge');
const asn1 = forge.asn1;
const pki = forge.pki;

/**
 * NLA layer of rdp stack
fork icon352
star icon0
watch icon0

+ 9 other calls in file

117
118
119
120
121
122
123
124
125
126
  const crt = fs.readFileSync(crtPath)
  const prikey = forge.pki.privateKeyFromPem(key.toString())
  const pubcrt = forge.pki.certificateFromPem(crt.toString())

  const p12Asn1 = forge.pkcs12.toPkcs12Asn1(prikey, pubcrt, password)
  const p12Der = forge.asn1.toDer(p12Asn1).getBytes()
  const p12b64 = forge.util.encode64(p12Der)

  return p12b64
}
fork icon333
star icon0
watch icon36

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const forge = require("node-forge");
const asn1 = forge.asn1;

// create a new ASN.1 sequence
const sequence = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
  // add an integer value
  asn1.create(
    asn1.Class.UNIVERSAL,
    asn1.Type.INTEGER,
    false,
    new Buffer([0x01])
  ),
  // add an OCTET STRING value
  asn1.create(
    asn1.Class.UNIVERSAL,
    asn1.Type.OCTETSTRING,
    false,
    new Buffer("Hello, world!", "utf8")
  ),
]);

// encode the ASN.1 structure to a DER-encoded binary buffer
const buffer = asn1.toDer(sequence);
console.log(buffer.toString("hex")); // output: "301b0201011a0d48656c6c6f2c20776f726c6421"

In this example, we first import the node-forge library and the asn1 module from it. We then create a new ASN.1 sequence using the asn1.create() method, which takes four arguments: the ASN.1 class, the ASN.1 type, a boolean value indicating whether the value is constructed or primitive, and the value itself. In this case, we create a sequence with an integer value (0x01) and an OCTET STRING value (Hello, world!). We then use the asn1.toDer() method to encode the ASN.1 structure to a DER-encoded binary buffer, which we log to the console in hexadecimal format.

188
189
190
191
192
193
194
195
196
    p12Asn1 = forge.pkcs12.toPkcs12Asn1(keys.privateKey, [cert, caCert], password, {
        algorithm: '3des',
    });
}

const p12Der = forge.asn1.toDer(p12Asn1).getBytes();
fs.writeFileSync(userP12Path, p12Der, {
    encoding: 'binary',
});
fork icon30
star icon76
watch icon11

64
65
66
67
68
69
70
71
72
73
}

function checkP12Password(file, password) {
    try {
        let p12Der = fs.readFileSync(file).toString('binary');
        let p12Asn1 = forge.asn1.fromDer(p12Der);
        forge.pkcs12.pkcs12FromAsn1(p12Asn1, password);
    } catch(err) {
        console.log(err.message);
        return false;
fork icon16
star icon49
watch icon11

+ 3 other calls in file

11
12
13
14
15
16
17
18
19
20
  const d = new Date().toISOString().slice(0, -5) + 'Z'
  return d
}

decodeKey(keyBase64Str) {
  const asn1 = forge.asn1.fromDer(forge.util.decode64(keyBase64Str))
  const pk = forge.pki.privateKeyFromAsn1(asn1)
  const pem = forge.pki.privateKeyToPem(pk)
  const key = new NodeRSA(pem, {
    scheme: 'pkcs1-sha256'
fork icon2
star icon26
watch icon2

220
221
222
223
224
225
226
227
228
    const p7 = forge.pkcs7.createEnvelopedData();
    p7.addRecipient(cert);
    p7.content = forge.util.createBuffer();
    p7.content.putString(value);
    p7.encrypt();
    const bytes = forge.asn1.toDer(p7.toAsn1()).getBytes();
    const raw = forge.util.encode64(bytes);
    return "ENC[PKCS7," + raw + "]";
}
fork icon0
star icon56
watch icon7

56
57
58
59
60
61
62
63
64
65
66
67
68
69
const readPkcs12Keystore = (path, password) => {
  const p12base64 = (0, _fs.readFileSync)(path, 'base64');


  const p12Der = _nodeForge.util.decode64(p12base64);


  const p12Asn1 = _nodeForge.asn1.fromDer(p12Der);


  const p12 = _nodeForge.pkcs12.pkcs12FromAsn1(p12Asn1, password);


  const keyObj = getKey(p12);
fork icon3
star icon0
watch icon0

88
89
90
91
92
93
94
95
96

// Getting the certificate
var filePath = path.resolve('Resource/testrest.p12');
var p12Buffer = fs.readFileSync(filePath);
var p12Der = forge.util.binary.raw.encode(new Uint8Array(p12Buffer));
var p12Asn1 = forge.asn1.fromDer(p12Der);
var certificate = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, keyPass);

// Getting the RSA private key
fork icon0
star icon0
watch icon1

+ 64 other calls in file

256
257
258
259
260
261
262
263
264
265

// Asssina em modo detached
p7.sign({detached: true});

// converta assinatura pra bytes e checa se o pdf tem um placeholder caiba a assinatura
const raw = forge.asn1.toDer(p7.toAsn1()).getBytes();
if ((raw.length * 2) > placeholderLength) {
    throw new SignPdfError(
        `Signature exceeds placeholder length: ${raw.length * 2} > ${placeholderLength}`,
        SignPdfError.TYPE_INPUT,
fork icon0
star icon0
watch icon1

+ 14 other calls in file