
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
@vatsdev/encryption-decryption
Advanced tools
A TypeScript library for secure encryption and decryption using Google Cloud KMS and Secret Manager
A TypeScript-based service that provides field-level encryption capabilities using Google Cloud KMS and Secret Manager.
GOOGLE_CLOUD_PROJECT
: Your GCP project IDGOOGLE_APPLICATION_CREDENTIALS
: Path to your service account key filesrc/
├── services/ # Core service implementations
│ ├── encryptionService.ts # Main encryption service
│ ├── kmsService.ts # Google Cloud KMS integration
│ └── secretManagerService.ts # Google Cloud Secret Manager integration
├── utils/ # Utility functions and helpers
│ ├── configUtils.ts # Configuration management
│ ├── encryptionUtils.ts # Encryption/decryption utilities
│ └── createHash.ts # Hashing utilities
├── types/ # TypeScript type definitions
│ ├── encryption.ts # Encryption-related types
│ └── errors.ts # Error types and codes
├── index.ts # Main entry point
├── functions.ts # Function exports
└── types.ts # Type exports
index.ts
)Contains core service implementations for:
Contains utility functions for:
Contains TypeScript type definitions for:
The service implements a comprehensive error handling system with custom error types:
ConfigurationError
: For initialization and configuration issuesEncryptionError
: For encryption/decryption failuresValidationError
: For input validation failuresEach error type includes specific error codes for better error tracking and handling.
The package uses the following environment variables:
CONFIG_PATH
: Path to your encryption configuration fileGOOGLE_CLOUD_PROJECT
: Your GCP project ID (required)GOOGLE_APPLICATION_CREDENTIALS
: Path to your service account key file (required)The encryption configuration file (encryption.json
) can be placed in one of these locations:
CONFIG_PATH
environment variable./config/encryption.json
./src/config/encryption.json
./encryption.json
Example configuration file:
{
"user": {
"firstName": {
"shouldEncrypt": true,
"shouldDecrypt": true,
"shouldHash": false,
"isObject": false,
"isArrayOfObjects": false
},
"lastName": {
"shouldEncrypt": true,
"shouldDecrypt": true,
"shouldHash": false,
"isObject": false,
"isArrayOfObjects": false
},
"phoneNumber": {
"shouldEncrypt": true,
"shouldDecrypt": true,
"shouldHash": false,
"isObject": false,
"isArrayOfObjects": false
}
},
"Company": {
"vatNumber": {
"shouldEncrypt": true,
"shouldDecrypt": true,
"shouldHash": false,
"isObject": false,
"isArrayOfObjects": false
},
"crNumber": {
"shouldEncrypt": true,
"shouldDecrypt": true,
"shouldHash": false,
"isObject": false,
"isArrayOfObjects": false
},
"user": {
"shouldEncrypt": false,
"shouldDecrypt": true,
"shouldHash": false,
"isObject": true,
"isArrayOfObjects": false
}
}
}
Each field in the configuration supports the following options:
shouldEncrypt
: Whether the field should be encryptedshouldDecrypt
: Whether the field should be decryptedshouldHash
: Whether to create a hash of the field valueisObject
: Whether the field is a nested objectisArrayOfObjects
: Whether the field is an array of objectsimport { encryptionService } from '@vatsdev/encryption-decryption-poc';
// Encrypt data
const result = await encryptionService.encryptObject({
modelName: 'user',
data: {
firstName: 'John',
lastName: 'Doe',
phoneNumber: '1234567890'
},
entityKeyDetailsResult: {
kmsPath: 'your-kms-path',
secretId: 'your-secret-id',
secretNamePath: 'your-secret-name-path',
encryptedDEK: Buffer.from('your-encrypted-dek')
}
});
// Decrypt data
const decrypted = await encryptionService.decryptObject({
modelName: 'User',
data: result.encryptedData,
entityKeyDetailsResult: result.keyMetadata
});
modelName
(string)The key identifier used in your encryption.json
configuration file. This should match the model name you've configured for encryption.
sensitiveData
(string)The data you want to encrypt. This can be any string value that needs to be protected.
entityKeyDetails
(object)An object containing all required fields for encryption and decryption operations:
interface EntityKeyDetails {
locationId: string | null; // Google Cloud KMS location ID
keyRingId: string | null; // KMS key ring identifier
keyId: string | null; // KMS key identifier
secretId: string | null; // Secret Manager secret ID
encryptedDEK?: Buffer | null; // Encrypted Data Encryption Key
keyVersion: string | null; // KMS key version
}
import encryptionService from './services/encryptionService';
// Encrypt a field
const encryptedField = await encryptionService.encryptField(
'model-name',
'sensitive-data',
entityKeyDetails
);
// Decrypt a field
const decryptedField = await encryptionService.decryptField(
'model-name',
'encrypted-data',
entityKeyDetails
);
npm install
npm test
MIT
FAQs
A TypeScript library for secure encryption and decryption using Google Cloud KMS and Secret Manager
The npm package @vatsdev/encryption-decryption receives a total of 167 weekly downloads. As such, @vatsdev/encryption-decryption popularity was classified as not popular.
We found that @vatsdev/encryption-decryption demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.