unpad static method
List<int>
unpad(
- List<
int> paddedData, - int blockSize, {
- PaddingAlgorithm style = PaddingAlgorithm.pkcs7,
Removes padding from the provided data.
This method removes padding from the input data, assuming it follows a specific padding style.
Parameters:
paddedData
: The padded data from which padding will be removed.blockSize
: The block size used for padding.style
: The padding style, which can be one of the PaddingAlgorithm values (default is pkcs7).
Returns:
- A new
List<int>
containing the input data with padding removed.
Throws:
Exception
for various scenarios, such as incorrect padding or zero-length input.
Implementation
static List<int> unpad(List<int> paddedData, int blockSize,
{PaddingAlgorithm style = PaddingAlgorithm.pkcs7}) {
final int paddedDataLen = paddedData.length;
if (paddedDataLen == 0) {
throw const ArgumentException('Zero-length input cannot be unpadded');
}
if (paddedDataLen % blockSize != 0) {
throw const ArgumentException('Input data is not padded');
}
int paddingLen;
if (style == PaddingAlgorithm.pkcs7 || style == PaddingAlgorithm.x923) {
paddingLen = paddedData[paddedDataLen - 1];
if (paddingLen < 1 || paddingLen > blockSize) {
throw const ArgumentException('incorrect padding');
}
if (style == PaddingAlgorithm.pkcs7) {
for (int i = 1; i <= paddingLen; i++) {
if (paddedData[paddedDataLen - i] != paddingLen) {
throw const ArgumentException('incorrect padding');
}
}
} else {
for (int i = 1; i < paddingLen; i++) {
if (paddedData[paddedDataLen - i - 1] != 0) {
throw const ArgumentException('incorrect padding');
}
}
}
} else {
final int index = paddedData.lastIndexOf(128);
if (index < 0) {
throw const ArgumentException('incorrect padding');
}
paddingLen = paddedDataLen - index;
if (paddingLen < 1 || paddingLen > blockSize) {
throw const ArgumentException('incorrect padding');
}
for (int i = 1; i < paddingLen; i++) {
if (paddedData[index + i] != 0) {
throw const ArgumentException('incorrect padding');
}
}
}
return paddedData.sublist(0, paddedDataLen - paddingLen);
}