Skip to content

Commit

Permalink
feat: remove external cipher dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
sgomez committed Jul 3, 2023
1 parent a1880c8 commit b58cf9a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
2 changes: 0 additions & 2 deletions src/eventstore-core.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CryptoModule } from '@akanass/nestjsx-crypto';
import {
DynamicModule,
Global,
Expand Down Expand Up @@ -26,7 +25,6 @@ import { KeyService, ProjectionsService, TransformerService } from './services';
@Global()
@Module({
imports: [
CryptoModule,
CqrsModule,
MongooseModule.forFeature(
[
Expand Down
13 changes: 7 additions & 6 deletions src/services/key.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { CryptoModule } from '@akanass/nestjsx-crypto';
import { getModelToken } from '@nestjs/mongoose';
import { Test } from '@nestjs/testing';
import { v4 as uuid } from 'uuid';
import { Model } from 'mongoose';
import { v4 as uuid } from 'uuid';

import { KeyDto, KeyDocument, KEYS } from '../crypto';
import { KEYS, KeyDocument, KeyDto } from '../crypto';
import { Event } from '../domain';
import { EVENTSTORE_KEYSTORE_CONNECTION } from '../eventstore.constants';
import { KeyService } from './key.service';

describe('KeyService', () => {
Expand All @@ -22,11 +22,10 @@ describe('KeyService', () => {

beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
imports: [CryptoModule],
providers: [
KeyService,
{
provide: getModelToken(KEYS),
provide: getModelToken(KEYS, EVENTSTORE_KEYSTORE_CONNECTION),
useValue: {
findById: jest.fn(),
findByIdAndRemove: jest.fn(),
Expand All @@ -36,7 +35,9 @@ describe('KeyService', () => {
}).compile();

keyService = moduleRef.get<KeyService>(KeyService);
keys = moduleRef.get<Model<KeyDocument>>(getModelToken(KEYS));
keys = moduleRef.get<Model<KeyDocument>>(
getModelToken(KEYS, EVENTSTORE_KEYSTORE_CONNECTION),
);
});

describe('encrypt', () => {
Expand Down
33 changes: 15 additions & 18 deletions src/services/key.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { AesService } from '@akanass/nestjsx-crypto';
import {
decryptWithAesKey,
encryptWithAesKey,
} from '@akanass/nestjsx-crypto/operators/aes';
import { Injectable, Logger } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { firstValueFrom } from 'rxjs';
import { v4 as uuid } from 'uuid';

import { KeyDocument, KeyDto, KEYS } from '../crypto';
import {
createAesKey,
decryptWithAesKey,
encryptWithAesKey,
KeyDocument,
KeyDto,
KEYS,
} from '../crypto';
import { Event } from '../domain';
import { KeyNotFoundError } from '../errors';
import { EVENTSTORE_KEYSTORE_CONNECTION } from '../eventstore.constants';
Expand All @@ -21,7 +22,6 @@ export class KeyService {
constructor(
@InjectModel(KEYS, EVENTSTORE_KEYSTORE_CONNECTION)
private readonly keys: Model<KeyDocument>,
private readonly aesService: AesService,
) {}

async create(id: string): Promise<KeyDto> {
Expand Down Expand Up @@ -56,11 +56,8 @@ export class KeyService {

const data = JSON.stringify(event.payload);

const source$ = this.aesService
.createKey(key.secret, key.salt)
.pipe(encryptWithAesKey(Buffer.from(data, 'utf16le')));

const buffer = await firstValueFrom(source$);
const aesKey = await createAesKey(key.secret, key.salt);
const buffer = encryptWithAesKey(Buffer.from(data, 'utf16le'), aesKey);

return event.withEncryptedPayload(buffer.toString('base64'));
}
Expand All @@ -72,11 +69,11 @@ export class KeyService {
throw KeyNotFoundError.withId(id);
}

const source$ = await this.aesService
.createKey(key.secret, key.salt)
.pipe(decryptWithAesKey(Buffer.from(encryptedPayload, 'base64')));

const buffer = await firstValueFrom(source$);
const aesKey = await createAesKey(key.secret, key.salt);
const buffer = decryptWithAesKey(
Buffer.from(encryptedPayload, 'base64'),
aesKey,
);
const payload = JSON.parse(buffer.toString('utf16le'));

return payload;
Expand Down

0 comments on commit b58cf9a

Please sign in to comment.