All files / src/crypto shared-secret.ts

100% Statements 7/7
100% Branches 4/4
100% Functions 1/1
100% Lines 7/7

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                        12x     12x   5x 5x 4x   5x   7x    
import {getCurve} from './curves'
 
/**
 * Derive shared secret for key pair.
 * @internal
 */
export function sharedSecret(
    privkey: Uint8Array,
    pubkey: Uint8Array,
    type: string,
    legacy = false
) {
    const curve = getCurve(type)
    const sharedPoint = curve.getSharedSecret(privkey, pubkey)
    const x = sharedPoint.slice(1, 33)
    if (legacy) {
        // strip leading zero bytes, replicating the elliptic-based derivation used before v2
        let offset = 0
        while (offset < x.length - 1 && x[offset] === 0) {
            offset++
        }
        return x.slice(offset)
    }
    return x
}