@alore/auth-react-native-sdk
Version:
React Native SDK for Alore Auth
47 lines (40 loc) • 1.16 kB
text/typescript
import crypto from 'crypto';
import argon2 from 'react-native-argon2';
import { AloreAuthError } from './AloreAuthError';
type KeyDerivationFunction = 'argon2d' | 'pbkdf2';
export async function generateSecureHash(
password: string,
salt: string,
keyDerivationFunction: KeyDerivationFunction = 'argon2d',
): Promise<string> {
try {
if (keyDerivationFunction === 'argon2d') {
const result = await argon2(password, salt, {
hashLength: 32,
memory: 32768,
iterations: 3,
parallelism: 2,
mode: 'argon2d',
});
return result.encodedHash;
}
throw new AloreAuthError(
'UNSUPPORTED_KEY_DERIVATION_FUNCTION',
'Unsupported key derivation function',
);
} catch (error) {
throw new AloreAuthError(
'UNSUPPORTED_KEY_DERIVATION_FUNCTION',
'Unsupported key derivation function',
);
}
}
export function hashUserInfo(userInfo: string) {
try {
const hash = crypto.createHash('sha256');
hash.update(userInfo);
return hash.digest('hex');
} catch (error) {
throw new AloreAuthError('HASH_USER_INFO_FAILED', 'Failed to hash user info');
}
}