Skip to main content

EncryptedData

Represents the EncryptedData structure described in RFC5652

Examples​

The following example demonstrates how to create and encrypt CMS Encrypted Data

const cmsEncrypted = new pkijs.EncryptedData();

await cmsEncrypted.encrypt({
contentEncryptionAlgorithm: {
name: "AES-GCM",
length: 256,
},
hmacHashAlgorithm: "SHA-256",
iterationCount: 1000,
password: password,
contentToEncrypt: dataToEncrypt,
});

// Add Encrypted Data into CMS Content Info
const cmsContent = new pkijs.ContentInfo();
cmsContent.contentType = pkijs.ContentInfo.ENCRYPTED_DATA;
cmsContent.content = cmsEncrypted.toSchema();

const cmsContentRaw = cmsContent.toSchema().toBER();

The following example demonstrates how to decrypt CMS Encrypted Data

// Parse CMS Content Info
const cmsContent = pkijs.ContentInfo.fromBER(cmsContentRaw);
if (cmsContent.contentType !== pkijs.ContentInfo.ENCRYPTED_DATA) {
throw new Error("CMS is not Encrypted Data");
}
// Parse CMS Encrypted Data
const cmsEncrypted = new pkijs.EncryptedData({ schema: cmsContent.content });

// Decrypt data
const decryptedData = await cmsEncrypted.decrypt({
password: password,
});

Extends​

Implements​

Constructors​

Constructor​

new EncryptedData(parameters?): EncryptedData

Initializes a new instance of the EncryptedData class

Parameters​

parameters?​

EncryptedDataParameters = {}

Initialization parameters

Returns​

EncryptedData

Overrides​

PkiObject.constructor

Properties​

encryptedContentInfo​

encryptedContentInfo: EncryptedContentInfo

Encrypted content information

Implementation of​

IEncryptedData.encryptedContentInfo


unprotectedAttrs?​

optional unprotectedAttrs?: Attribute[]

Collection of attributes that are not encrypted

Implementation of​

IEncryptedData.unprotectedAttrs


version​

version: number

Version number.

If unprotectedAttrs is present, then the version MUST be 2. If unprotectedAttrs is absent, then version MUST be 0.

Implementation of​

IEncryptedData.version


CLASS_NAME​

static CLASS_NAME: string = "EncryptedData"

Name of the class

Overrides​

PkiObject.CLASS_NAME

Accessors​

className​

Get Signature​

get className(): string

Returns​

string

Inherited from​

PkiObject.className

Methods​

decrypt()​

decrypt(parameters, crypto?): Promise<ArrayBuffer>

Creates a new CMS Encrypted Data content

Parameters​

parameters​

Parameters necessary for encryption

password​

ArrayBuffer

crypto?​

ICryptoEngine = ...

Crypto engine

Returns​

Promise<ArrayBuffer>

Returns decrypted raw data


encrypt()​

encrypt(parameters, crypto?): Promise<void>

Creates a new CMS Encrypted Data content

Parameters​

parameters​

EncryptedDataEncryptParams

Parameters necessary for encryption

crypto?​

ICryptoEngine = ...

Returns​

Promise<void>


fromSchema()​

fromSchema(schema): void

Converts parsed ASN.1 object into current class

Parameters​

schema​

any

ASN.1 schema

Returns​

void

Overrides​

PkiObject.fromSchema


toJSON()​

toJSON(): EncryptedDataJson

Converts the class to JSON object

Returns​

EncryptedDataJson

JSON object

Overrides​

PkiObject.toJSON


toSchema()​

toSchema(): Sequence

Converts current object to ASN.1 object and sets correct values

Returns​

Sequence

ASN.1 object

Overrides​

PkiObject.toSchema


toString()​

toString(encoding?): string

Parameters​

encoding?​

"hex" | "base64" | "base64url"

Returns​

string

Inherited from​

PkiObject.toString


blockName()​

static blockName(): string

Returns block name

Returns​

string

Returns string block name

Inherited from​

PkiObject.blockName


compareWithDefault()​

static compareWithDefault(memberName, memberValue): boolean

Compare values with default values for all class members

Parameters​

memberName​

string

String name for a class member

memberValue​

any

Value to compare with default value

Returns​

boolean


defaultValues()​

Call Signature​

static defaultValues(memberName): number

Returns default values for all class members

Parameters​
memberName​

"version"

String name for a class member

Returns​

number

Default value

Overrides​

PkiObject.defaultValues

Call Signature​

static defaultValues(memberName): EncryptedContentInfo

Returns default values for all class members

Parameters​
memberName​

"encryptedContentInfo"

String name for a class member

Returns​

EncryptedContentInfo

Default value

Overrides​

PkiObject.defaultValues

Call Signature​

static defaultValues(memberName): Attribute[]

Returns default values for all class members

Parameters​
memberName​

"unprotectedAttrs"

String name for a class member

Returns​

Attribute[]

Default value

Overrides​

PkiObject.defaultValues


fromBER()​

static fromBER<T>(this, raw): T

Creates PKI object from the raw data

Type Parameters​

T​

T extends PkiObject

Parameters​

this​

PkiObjectConstructor<T>

raw​

BufferSource

ASN.1 encoded raw data

Returns​

T

Initialized and filled current class object

Inherited from​

PkiObject.fromBER


schema()​

static schema(parameters?): any

Returns value of pre-defined ASN.1 schema for current class

Parameters​

parameters?​

SchemaParameters<{ encryptedContentInfo?: EncryptedContentInfoSchema; unprotectedAttrs?: string; version?: string; }> = {}

Input parameters for the schema

Returns​

any

ASN.1 schema object

Overrides​

PkiObject.schema