Skip to content

infinity.crypto

Provides shared types and enumerations for cryptographic operations. This namespace contains common types used by the cryptographic namespaces infinity.md5, infinity.sha1, infinity.sha256, infinity.sha512 and infinity.rsa.

Loading the module infinity.crypto registers the namespaces infinity.crypto, infinity.random, infinity.md5, infinity.sha1, infinity.sha256, infinity.sha512, infinity.aes256, infinity.rsa and infinity.bcrypt.

Module: infinity.crypto

Functions:
Enumerations:

Functions

infinity.sha1.hmac()

Returns a keyed hash value (HMAC-SHA1) of the given string. Message and key are encoded as UTF-8. An empty value or key is used as is (before 3.11: an empty string was returned). This is the only function of the infinity.sha1 namespace; there is no plain SHA-1 hash function.

Signature:

hmac( value: string, key: string, outputFormat?: infinity.crypto.outputFormat ): string

Parameters:

  • value: string

    Message to be hashed.

  • key: string

    Shared secret key used for generating the HMAC variant of the message digest.

  • outputFormat: infinity.crypto.outputFormat, optional

    The format of the result: infinity.crypto.outputFormat.hex (lowercase hexadecimal, default) or infinity.crypto.outputFormat.base64.

Return type: string

Example:

infinity.loadModule('infinity.crypto');
let hmac = infinity.sha1.hmac('message', 'secretKey');
console.debug(hmac);
//b5fd3df92abdaeefa8c973f771612c9fcb9da2fd

Enumerations

outputFormat

Specifies the output format for cryptographic hash and HMAC functions.

Values:

  • hex: 0 Output as hexadecimal string (lowercase). This is the default for all functions with an outputFormat parameter.

Example: "5d41402abc4b2a76b9719d911017c592"

  • base64: 1 Output as Base64-encoded string (standard alphabet with = padding, no line breaks).

Example: "XUFAKrxLKna5cZ2REBfFkg=="

Used by:

Example:

infinity.loadModule('infinity.crypto');

// Hash with hexadecimal output (default)
let hexHash = infinity.md5.hash('Hello World', infinity.crypto.outputFormat.hex);
console.log(hexHash);  // "b10a8db164e0754105b7a99be72e3fe5"

// Hash with base64 output
let base64Hash = infinity.md5.hash('Hello World', infinity.crypto.outputFormat.base64);
console.log(base64Hash);  // "sQqNsWTgdUEFt6mb5y4/5Q=="

algorithm

Specifies the hash algorithm used for RSA signatures created and verified with hashAndSign() and hashAndVerify(). If the algorithm is omitted, these functions use sha1.

Values:

  • sha1: 0 SHA-1 (160-bit). Note: SHA-1 is considered weak for security-critical applications. Use SHA-256 or higher for new applications.

  • sha224: 1 SHA-224 (224-bit). Part of the SHA-2 family.

  • sha256: 2 SHA-256 (256-bit). Recommended for most applications. Part of the SHA-2 family.

  • sha384: 3 SHA-384 (384-bit). Part of the SHA-2 family.

  • sha512: 4 SHA-512 (512-bit). Part of the SHA-2 family. Provides the highest security level.

  • md5: 5 MD5 (128-bit). Note: MD5 is cryptographically broken and should not be used for security purposes. Use only for checksums or non-security applications.

Used by:

Example:

infinity.loadModule('infinity.crypto');

// Generate RSA key pair
let keys = infinity.rsa.generateKeys(2048);

// Sign data using SHA-256 (recommended)
let data = 'Important message';
let signature = infinity.rsa.hashAndSign(data, keys.private, infinity.crypto.algorithm.sha256);

// Verify signature
let isValid = infinity.rsa.hashAndVerify(data, keys.public, signature, infinity.crypto.algorithm.sha256);
console.log('Signature valid: ' + isValid);  // true

Security Recommendations

Hash Algorithm Selection

For different use cases, choose appropriate algorithms:

Digital Signatures and Certificates: - Use SHA-256 or SHA-512 for production systems - Avoid SHA-1 and MD5 for security-critical applications

Password Hashing: - Use infinity.bcrypt instead of regular hash functions - Bcrypt includes salt and is designed specifically for password storage

File Integrity and Checksums: - SHA-256 for security-sensitive files - MD5 acceptable for non-security checksums (e.g., detecting file corruption)

HMAC (Message Authentication): - Use SHA-256 or SHA-512 - Higher bit algorithms (SHA-512) provide better security for long-term applications

Output Format Selection

Hexadecimal (hex): - Human-readable - Commonly used in debugging and logging - Direct comparison with other tools - 2x longer than base64 for the same data

Base64: - More compact (about one third shorter than hex) - Better for transmission over networks or storage - Required for many APIs and protocols

Example showing both formats:

infinity.loadModule('infinity.crypto');

let data = 'Sensitive data';

// Hex output - good for logging and debugging
let hexHash = infinity.sha256.hash(data, infinity.crypto.outputFormat.hex);
console.log('Hex: ' + hexHash);
// Output: "Hex: 474de7276ecc120b83bc42291a96a36c648a0f6428bf7c88bfdccf32bfbb4339"

// Base64 output - good for APIs and compact storage
let base64Hash = infinity.sha256.hash(data, infinity.crypto.outputFormat.base64);
console.log('Base64: ' + base64Hash);
// Output: "Base64: R03nJ27MEguDvEIpGpajbGSKD2Qov3yIv9zPMr+7Qzk="

The infinity.crypto namespace provides types used by these cryptographic modules:


Complete Example

infinity.loadModule('infinity.crypto');

// === Hash Functions with Different Output Formats ===

let message = 'Hello, World!';

// MD5 (for checksums only, not security)
let md5Hex = infinity.md5.hash(message, infinity.crypto.outputFormat.hex);
let md5Base64 = infinity.md5.hash(message, infinity.crypto.outputFormat.base64);

console.log('MD5 (hex): ' + md5Hex);
console.log('MD5 (base64): ' + md5Base64);

// SHA-256 (recommended for most uses)
let sha256Hex = infinity.sha256.hash(message, infinity.crypto.outputFormat.hex);
let sha256Base64 = infinity.sha256.hash(message, infinity.crypto.outputFormat.base64);

console.log('SHA-256 (hex): ' + sha256Hex);
console.log('SHA-256 (base64): ' + sha256Base64);

// === RSA Digital Signatures with Different Algorithms ===

let keys = infinity.rsa.generateKeys(2048);

// Sign with SHA-256 (recommended)
let signatureSHA256 = infinity.rsa.hashAndSign(
    message,
    keys.private,
    infinity.crypto.algorithm.sha256
);

// Sign with SHA-512 (higher security)
let signatureSHA512 = infinity.rsa.hashAndSign(
    message,
    keys.private,
    infinity.crypto.algorithm.sha512
);

// Verify signatures
let valid256 = infinity.rsa.hashAndVerify(
    message,
    keys.public,
    signatureSHA256,
    infinity.crypto.algorithm.sha256
);

let valid512 = infinity.rsa.hashAndVerify(
    message,
    keys.public,
    signatureSHA512,
    infinity.crypto.algorithm.sha512
);

console.log('SHA-256 signature valid: ' + valid256);
console.log('SHA-512 signature valid: ' + valid512);

// === HMAC with Different Output Formats ===

let secretKey = 'my-secret-key';

let hmacHex = infinity.sha256.hmac(
    message,
    secretKey,
    infinity.crypto.outputFormat.hex
);

let hmacBase64 = infinity.sha256.hmac(
    message,
    secretKey,
    infinity.crypto.outputFormat.base64
);

console.log('HMAC (hex): ' + hmacHex);
console.log('HMAC (base64): ' + hmacBase64);

Best Practices

  1. Use appropriate algorithms:
  2. SHA-256 or SHA-512 for security-sensitive operations
  3. Avoid MD5 and SHA-1 for cryptographic purposes
  4. Use bcrypt specifically for password hashing

  5. Choose output format based on use case:

  6. Hex for debugging, logging, and human readability
  7. Base64 for APIs, network transmission, and compact storage

  8. Keep cryptographic libraries updated:

  9. INFINITY.JS includes up-to-date cryptographic implementations
  10. Review security advisories for your application domain

  11. Use appropriate key sizes:

  12. RSA: minimum 2048 bits, 4096 bits for long-term security
  13. AES: use AES-256 (provided by infinity.aes256)

  14. Never implement custom cryptography:

  15. Use the provided modules instead of creating custom algorithms
  16. Cryptography is difficult to implement correctly