mirror of
https://github.com/immich-app/immich.git
synced 2026-03-04 09:57:33 +03:00
chore: migrate database files (#8126)
This commit is contained in:
44
server/src/repositories/crypto.repository.ts
Normal file
44
server/src/repositories/crypto.repository.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { compareSync, hash } from 'bcrypt';
|
||||
import { createHash, randomBytes, randomUUID } from 'node:crypto';
|
||||
import { createReadStream } from 'node:fs';
|
||||
import { Instrumentation } from 'src/infra/instrumentation';
|
||||
import { ICryptoRepository } from 'src/interfaces/crypto.repository';
|
||||
|
||||
@Instrumentation()
|
||||
@Injectable()
|
||||
export class CryptoRepository implements ICryptoRepository {
|
||||
randomUUID() {
|
||||
return randomUUID();
|
||||
}
|
||||
|
||||
randomBytes(size: number) {
|
||||
return randomBytes(size);
|
||||
}
|
||||
|
||||
hashBcrypt(data: string | Buffer, saltOrRounds: string | number) {
|
||||
return hash(data, saltOrRounds);
|
||||
}
|
||||
|
||||
compareBcrypt(data: string | Buffer, encrypted: string) {
|
||||
return compareSync(data, encrypted);
|
||||
}
|
||||
|
||||
hashSha256(value: string) {
|
||||
return createHash('sha256').update(value).digest('base64');
|
||||
}
|
||||
|
||||
hashSha1(value: string | Buffer): Buffer {
|
||||
return createHash('sha1').update(value).digest();
|
||||
}
|
||||
|
||||
hashFile(filepath: string | Buffer): Promise<Buffer> {
|
||||
return new Promise<Buffer>((resolve, reject) => {
|
||||
const hash = createHash('sha1');
|
||||
const stream = createReadStream(filepath);
|
||||
stream.on('error', (error) => reject(error));
|
||||
stream.on('data', (chunk) => hash.update(chunk));
|
||||
stream.on('end', () => resolve(hash.digest()));
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user