rwadurian/backend/services/admin-service/src/domain/value-objects/file-sha256.vo.ts

44 lines
996 B
TypeScript

import { DomainException } from '../../shared/exceptions/domain.exception';
/**
* 文件 SHA256 哈希值对象
*/
export class FileSha256 {
private readonly _value: string;
private constructor(value: string) {
this._value = value;
}
get value(): string {
return this._value;
}
static create(value: string): FileSha256 {
if (!value || value.trim() === '') {
throw new DomainException('FileSha256 cannot be empty');
}
const trimmed = value.trim().toLowerCase();
// SHA256 哈希值长度为 64 个十六进制字符
if (trimmed.length !== 64) {
throw new DomainException('FileSha256 must be 64 characters long');
}
if (!/^[a-f0-9]{64}$/.test(trimmed)) {
throw new DomainException('FileSha256 must contain only hexadecimal characters');
}
return new FileSha256(trimmed);
}
equals(other: FileSha256): boolean {
return this._value === other._value;
}
toString(): string {
return this._value;
}
}