All files / src/crypter/padding ISO10126Padding.ts

100% Statements 6/6
100% Branches 0/0
100% Functions 2/2
100% Lines 6/6

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 161x     1x     147x 147x       139x 139x      
import randomBytes from 'randombytes';
import type { IPadding } from './IPadding';
 
export class ISO10126Padding implements IPadding {
 
    public addTo(data: Uint8Array, blockSize: number): Uint8Array {
        const paddingLength = blockSize - data.length % blockSize;
        return new Uint8Array([...data, ...randomBytes(paddingLength - 1), paddingLength]);
    }
 
    public removeFrom(data: Uint8Array): Uint8Array {
        const paddingLength = data[data.length - 1];
        return data.slice(0, data.length - paddingLength);
    }
}