Created
January 17, 2021 06:32
-
-
Save hacker1024/9cdd4d29c49523e3117c8fa8126e3a66 to your computer and use it in GitHub Desktop.
PKCS #5 padding in Dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:typed_data'; | |
| Uint8List padPKCS5(List<int> input) { | |
| final inputLength = input.length; | |
| final paddingValue = 8 - (inputLength % 8); | |
| final outputLength = inputLength + paddingValue; | |
| final output = Uint8List(outputLength); | |
| for (var i = 0; i < inputLength; ++i) { | |
| output[i] = input[i]; | |
| } | |
| output.fillRange(outputLength - paddingValue, outputLength, paddingValue); | |
| return output; | |
| } | |
| int getPKCS5PadCount(List<int> input) { | |
| if (input.length % 8 != 0) { | |
| throw FormatException('Block size is invalid!', input); | |
| } | |
| final count = input.last; | |
| final paddingStartIndex = input.length - count; | |
| for (var i = input.length - 1; i >= paddingStartIndex; --i) { | |
| if (input[i] != count) { | |
| throw FormatException('Padding is not valid PKCS5 padding!'); | |
| } | |
| } | |
| return count; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I license this under the
.