From 40a42789d1cb1fb01f0c51bd9a222a0760ac9917 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sat, 28 Nov 2020 22:08:50 -0500 Subject: lib: blocks: add Pkcs7Padding --- lib/blocks.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'lib') diff --git a/lib/blocks.go b/lib/blocks.go index 9436d3c..1ba811f 100644 --- a/lib/blocks.go +++ b/lib/blocks.go @@ -60,3 +60,16 @@ func BlocksEqual(a, b []byte) bool { } return true } + +// Performs PKCS#7 Padding on the input `in` and block size `k`. +// Assumes 0 > `k` < 256 +// Reference: https://tools.ietf.org/html/rfc5652#section-6.3 +func Pkcs7Padding(in []byte, k int) []byte { + lth := len(in) + pd := k - (lth % k) // padding character and padding length + + for i := 0; i < pd; i++ { + in = append(in, byte(pd)) + } + return in +} -- cgit v1.2.3