N/crypto/certificate Module Script Samples
The following script samples demonstrate how to use the features of the N/crypto/certificate module:
Load an XML File from the File Cabinet and Sign It Using a Digital Certificate
This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.
The following sample loads an XML file from the File Cabinet and signs it using the digital certificate with internal ID ‘custcertificate1’. Note that this sample uses a hard-coded value for the file id. You should change this value to a valid file id from your account.
This sample script uses the require
function so that you can copy it into the SuiteScript Debugger and test it. You must use the define
function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.
This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.
/**
* @NApiVersion 2.1
*/
require(['N/crypto/certificate','N/file'],(certificate, file) => {
// Load the file from the File Cabinet.
// Note that the id value is hard-coded in this sample, and you should use
// a valid file id from your account.
let infNFe = file.load({
id: 922
});
let signedXml = certificate.signXml({
algorithm: certificate.HashAlg.SHA256,
certId: 'custcertificate1',
rootTag: 'infNFe',
xmlString: infNFe.getContents()
});
certificate.verifyXMLSignature({
signedXml:signedXml,
rootTag: 'infNFe'
});
});
Create Signer and Verifier Objects
This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.
The following sample creates a certificate.Signer object, signs it, and then creates a certificate.Verifier object and verifies the signer object.
This sample script uses the require
function so that you can copy it into the SuiteScript Debugger and test it. You must use the define
function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.
This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.
/**
* @NApiVersion 2.1
*/
require(['N/crypto/certificate'], (certificate) => {
let signer = certificate.createSigner({
certId: 'custcertificate1',
algorithm: certificate.HashAlg.SHA256
});
signer.update('test');
let result = signer.sign();
let verifier = certificate.createVerifier({
certId: 'custcertificate1',
algorithm: certificate.HashAlg.SHA256
});
verifier.update('test');
verifier.verify(result);
})