Skip to main content

Key Integrity Validation

validateKeyIntegrity()​

const validateKeyIntegrity = (keyAlias?: string): Promise<KeyIntegrityResult> => {
};

Performs comprehensive validation of key integrity including format checks, accessibility tests, signature validation, and hardware backing verification. On Android API 31+, the result also reports strongBoxBacked in integrityChecks when the backing type can be distinguished.

If the key requires authentication, the signature test shows a prompt first. On Android, it can show a second prompt when Keystore discards the signing operation, as described under signWithOptions() below. If that attempt fails too, signatureTestPassed is false.

verifyKeySignature()​

const verifyKeySignature = (
keyAlias: string = '', // pass '' to use the default (configured) alias
data: string,
promptTitle?: string,
promptSubtitle?: string,
cancelButtonText?: string,
returnAuthType?: boolean
): Promise<SignatureResult> => {
};

Generates a cryptographic signature for the provided data using the specified key.

  • data: The data to be signed.
  • keyAlias (optional): The alias of the key to use for signing.
  • promptTitle (optional): Title text displayed in the signature prompt dialog.
  • promptSubtitle (optional): Subtitle text providing additional context in the prompt dialog (Android only).
  • cancelButtonText (optional): Text for the cancel button in the prompt dialog (Android only).
  • returnAuthType (optional): When true, includes authType in the result indicating how the user authenticated.
  • Returns a SignatureResult with success, signature, error, errorCode, and optionally authType (when returnAuthType is true).

signWithOptions()​

Signs data with advanced security controls, including the ability to disable device credential fallback.

import { InputEncoding, BiometricStrength } from '@sbaiahmed1/react-native-biometrics';

type SignatureOptions = {
keyAlias?: string; // Key alias to use for signing
data: string; // Data to sign
inputEncoding?: InputEncoding; // Input encoding: InputEncoding.UTF8 (default) or InputEncoding.Base64
promptTitle?: string; // Prompt title
promptSubtitle?: string; // Prompt subtitle (Android only)
cancelButtonText?: string; // Cancel button text
biometricStrength?: BiometricStrength; // Android only; no effect on the prompt (see below)
disableDeviceFallback?: boolean; // Prevent PIN/pattern fallback (Android only)
returnAuthType?: boolean; // Include authType in result (see AuthType enum)
};

// InputEncoding enum values:
// - InputEncoding.UTF8: Data is treated as a UTF-8 string (default)
// - InputEncoding.Base64: Data is decoded from base64 before signing (for WebAuthn/binary data)

biometricStrength has no effect on the signing prompt. Android Keystore keys can only be unlocked by Class 3 (strong) biometrics or the device credential (KeyProperties has no weak-biometric auth type), and a BiometricPrompt that carries a CryptoObject throws if Class 2 (weak) biometrics are allowed. signWithOptions therefore shows the authenticators the key was created with, whatever you pass here. Camera-based face unlock is Class 2 on most phones, so it does not appear in the signing prompt even with BiometricStrength.Weak; fingerprint does, and so does Class 3 face unlock on the few phones that have it. To let face-unlock users sign anyway, authenticate them first with authenticateWithOptions({ biometricStrength: BiometricStrength.Weak }) and then call sign() on a key created with createKeysWithOptions({ requireAuthentication: false }). That signature is tied to the biometric only by your app's flow, not by the hardware, so treat it as a weaker guarantee.

Android can show a second prompt. Some devices, StrongBox phones in particular, discard the signing operation when the prompt is open too long, so signing fails even though the user authenticated. signWithOptions then shows the prompt once more, with the same title and subtitle, on a new signing operation. Canceling it returns USER_CANCELED. If Keystore discards the operation again, the result has errorCode: 'SIGNATURE_OPERATION_EXPIRED'. verifyKeySignature() behaves the same way.

Example - Requiring biometrics only (no PIN fallback):

import { signWithOptions, BiometricStrength, InputEncoding } from '@sbaiahmed1/react-native-biometrics';

// High-security signing - biometrics only, no PIN fallback
const result = await signWithOptions({
keyAlias: 'transaction_key',
data: JSON.stringify({ amount: 1000, recipient: 'user123' }),
promptTitle: 'Authorize Transaction',
promptSubtitle: 'Use biometrics to sign this transaction',
biometricStrength: BiometricStrength.Strong,
disableDeviceFallback: true, // Fail if biometrics unavailable
});

if (result.success) {
console.log('Signature:', result.signature);
} else if (result.errorCode === 'BIOMETRIC_NOT_AVAILABLE') {
console.log('Biometrics required but not available');
}

When to use disableDeviceFallback: true:

  • High-value financial transactions
  • Multi-factor authentication where biometric is the second factor
  • Operations requiring proof of physical presence
  • Security-critical actions that must not fall back to knowledge-based authentication

Using base64 input for WebAuthn/binary data:

import { signWithOptions, InputEncoding } from '@sbaiahmed1/react-native-biometrics';

// When your challenge is binary data (e.g., from WebAuthn)
const binaryChallenge = 'SGVsbG8gV29ybGQh'; // base64-encoded bytes

const result = await signWithOptions({
keyAlias: 'webauthn_key',
data: binaryChallenge,
inputEncoding: InputEncoding.Base64, // Decode as binary, not UTF-8
promptTitle: 'Authenticate',
});

This avoids double-encoding issues when working with WebAuthn challenges or other binary data that's already base64-encoded.

validateSignature()​

const validateSignature = (
keyAlias: string = '', // pass '' to use the default (configured) alias
data: string,
signature: string
): Promise<SignatureValidationResult> => {
};

Validates a signature against the original data using the public key.

sha256()​

Computes a SHA-256 hash of the provided data using native platform cryptography.

const sha256 = (
data: string,
inputEncoding?: 'utf8' | 'base64'
): Promise<Sha256Result> => {
};

type Sha256Result = {
hash: string; // Base64-encoded SHA-256 hash
error?: string; // Error message if hashing failed
}

Parameters:

  • data: The data to hash.
  • inputEncoding (optional, default 'utf8'): How to interpret the input data. Use 'base64' if the data is already base64-encoded binary.

Example:

import { sha256 } from '@sbaiahmed1/react-native-biometrics';

const result = await sha256('Hello, world!');
console.log('Hash:', result.hash); // Base64-encoded SHA-256 hash

getKeyAttributes()​

const getKeyAttributes = (keyAlias?: string): Promise<KeyAttributesResult> => {
};

Retrieves detailed attributes and security properties of the specified key.

Example:

import {
validateKeyIntegrity,
verifyKeySignature,
validateSignature,
getKeyAttributes
} from '@sbaiahmed1/react-native-biometrics';

// Validate key integrity
const integrityResult = await validateKeyIntegrity('my-key');
console.log('Key valid:', integrityResult.valid);
console.log('Hardware backed:', integrityResult.integrityChecks.hardwareBacked);
console.log('StrongBox backed:', integrityResult.integrityChecks.strongBoxBacked); // Android API 31+ only

// Generate and validate signature
const data = 'Hello, secure world!';
const signatureResult = await verifyKeySignature('my-key', data);
if (signatureResult.success && signatureResult.signature) {
const validationResult = await validateSignature('my-key', data, signatureResult.signature);
console.log('Signature valid:', validationResult.valid);
}

// Get key attributes
const attributes = await getKeyAttributes('my-key');
if (attributes.exists) {
console.log('Algorithm:', attributes.attributes.algorithm);
console.log('Key size:', attributes.attributes.keySize);
console.log('Security level:', attributes.attributes.securityLevel);
}