Установим зависимости для NodeJS
npm i openpgp --save
npm i @openpgp/web-stream-tools --save-dev
Сниппет работы
import * as openpgp from 'openpgp'
const passwords = ['...'] // todo установите нужный пароль
export const openpgpEncrypt = async (buffer: Buffer) => {
if (Buffer.byteLength(buffer) === 0) {
throw new Error('Empty buffer')
}
const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(buffer),
passwords,
compression: openpgp.enums.compression.zlib,
})
return encrypted
}
export const openpgpDecrypt = async (buffer: Buffer) => {
const utf8Content = buffer.toString('utf8')
if (utf8Content.startsWith('-----BEGIN PGP MESSAGE-----')) {
const rawDecrypt = await openpgp.decrypt({
message: await openpgp.message.readArmored(utf8Content),
passwords,
format: 'binary',
})
return rawDecrypt.data
}
return utf8Content
}
Пример запуска
const pgpEncrypted = await openpgpEncrypt(Buffer.from('Hello world'))
const pgpDecrypted = await openpgpDecrypt(Buffer.from(pgpEncrypted))