All files / lib/util encodingUtils.ts

90% Statements 9/10
100% Branches 0/0
66.67% Functions 2/3
90% Lines 9/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3427x                         27x   26x     26x     26x     26x     27x 26x     27x    
import crypto from 'crypto';
 
/**
 * Create a NON-CRYPTOGRAPHIC (INSECURE) hash of some pipeline content.
 * Hash algorithm should be strong enough to use for caching, but does not need to be cryptographically secure.
 * This may be called many times by the pipeline, so the algorithm used should be reasonably fast as well.
 *
 * Standard implementation uses MD5 as provided by the Node.JS Crypto module.
 * Override to change implementation
 *
 * @param content Content to hash. Should be a UTF-8 string.
 * @returns Returns a hash of content as a Base64 string
 */
export function hashMD5(content: string): string {
    // create hash instance
    const md5 = crypto.createHash('md5');
 
    // load the content
    md5.update(content, 'utf8');
 
    // calculate the hash
    const b64 = md5.digest('base64');
 
    // Convert to path-safe Base64
    return base64ToPathSafeBase64(b64);
}
 
export function base64ToPathSafeBase64(base64: string): string {
    return base64.replace(/\//g, '_');
}
 
export function pathSafeBas64ToBase64(pathSafeBase64: string): string {
    return pathSafeBase64.replace(/_/g, '/');
}