feat(crypto): add certificate hash generation and verification functions

- Implemented `generateCertificateHash` to create a hash using HMAC-SHA256 based on user ID and team ID.
- Added `verifyCertificateHash` to validate a given certificate ID against generated hash.
- Introduced `crypto-js` library as a dependency for cryptographic functions.

chore(deps): update ajv to version 8.13.0 and add crypto-js dependency

- Updated `ajv` from version 6.12.6 to 8.13.0 in package.json and package-lock.json.
- Added `crypto-js` library to package.json for cryptographic operations.
- Included type definitions for `crypto-js` in development dependencies.
This commit is contained in:
xirf
2025-12-06 17:35:30 +07:00
parent a4c75770ae
commit ae3dd8c3f1
9 changed files with 5052 additions and 131 deletions
+2
View File
@@ -7,3 +7,5 @@ export * from './cookies';
export * from './session';
export * from './constants';
export * from './logic';
export * from './lib/crypto';
export * from './lib/certificate';
+18
View File
@@ -0,0 +1,18 @@
import { generateCertificateHash } from './crypto';
/**
* Generate a certificate URL for a user and team
* @param userId - User ID
* @param teamId - Team ID
* @param baseUrl - Base URL (optional, defaults to current origin)
* @returns Certificate URL
*/
export const generateCertificateUrl = (
userId: string,
teamId: string,
baseUrl?: string
): string => {
const certId = generateCertificateHash(userId, teamId);
const base = baseUrl || (typeof window !== 'undefined' ? window.location.origin : '');
return `${base}/certificate/${certId}`;
};
+31
View File
@@ -0,0 +1,31 @@
import CryptoJS from 'crypto-js';
/**
* Generate certificate hash using HMAC-SHA256
* @param userId - User ID
* @param teamId - Team ID
* @returns Hash string
*/
export const generateCertificateHash = (userId: string, teamId: string): string => {
const key = 'imphnenxkolosal';
const message = userId + teamId;
// Repeat key to match required length (32 bytes for SHA256)
const repeatedKey = key.repeat(Math.ceil(32 / key.length)).substring(0, 32);
// Create HMAC-SHA256 hash
const hash = CryptoJS.HmacSHA256(message, repeatedKey);
return hash.toString(CryptoJS.enc.Hex);
};
/**
* Verify if a certificate hash is valid for a given user and team
* @param certId - Certificate ID to verify
* @param userId - User ID
* @param teamId - Team ID
* @returns True if the certificate is valid
*/
export const verifyCertificateHash = (certId: string, userId: string, teamId: string): boolean => {
const generatedHash = generateCertificateHash(userId, teamId);
return generatedHash === certId;
};