How to use the pkcs12 function from node-forge

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

116
117
118
119
120
121
122
123
124
125
const key = fs.readFileSync(keyPath)
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

257
258
259
260
261
262
263
264
265
266
// we're only handling the common case of file import/export password (integrity)
// being the same as encrypted private key passphrase (privacy)
// see -twopass arg for openssl: https://linux.die.net/man/1/pkcs12
let pkcs12Obj = {};
if (typeof options.importPassword === 'undefined') {
    pkcs12Obj = forge.pkcs12.pkcs12FromAsn1(pkcs12Asn1);
} else {
    pkcs12Obj = forge.pkcs12.pkcs12FromAsn1(pkcs12Asn1, options.importPassword);
}
return {
fork icon56
star icon139
watch icon51

174
175
176
177
178
179
180
181
182
183
fs.outputFileSync(userKeyPath, privateKeyPem);

let p12Asn1;
//create .p12 file
if (rootCaCert) {
    p12Asn1 = forge.pkcs12.toPkcs12Asn1(
        keys.privateKey,
        [cert, caCert, rootCaCert],
        password,
        {
fork icon30
star icon76
watch icon11

+ 3 other calls in file

65
66
67
68
69
70
71
72
73
74

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

58
59
60
61
62
63
64
65
66
67

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

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

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

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

89
90
91
92
93
94
95
96
97
98
// 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

// get rsa key bags
fork icon0
star icon0
watch icon1

+ 64 other calls in file

77
78
79
80
81
82
83
84
85
86
const exportedAsBase64 = window.btoa(exportedAsString);
const pemExported = `-----BEGIN PRIVATE KEY-----\n${exportedAsBase64}\n-----END PRIVATE KEY-----`;
// lê chave privada de um arquivo
const privateKey = forge.pki.privateKeyFromPem(pemExported);
// Cria um pkcs12 com a chave privada e os certificados do pkcs7 recebido
const p12Asn1 = forge.pkcs12.toPkcs12Asn1(privateKey, pkcs7.certificates, '');

// Lê pkcs12
var pkcs12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, "");
// Assina o PDF
fork icon0
star icon0
watch icon1

+ 163 other calls in file