verification when decoding pkcs5
This commit is contained in:
parent
40a8c54342
commit
5a6e65c00f
|
@ -24,11 +24,8 @@
|
||||||
*/
|
*/
|
||||||
function encode(msg) {
|
function encode(msg) {
|
||||||
const c = 8 - (msg.length % 8);
|
const c = 8 - (msg.length % 8);
|
||||||
var result = [];
|
const padding = String.fromCharCode(c).repeat(c);
|
||||||
for (var i = 0; i < c; ++i) {
|
return msg + padding;
|
||||||
result.push(String.fromCharCode(c));
|
|
||||||
}
|
|
||||||
return msg + result.join("");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -37,11 +34,15 @@ function encode(msg) {
|
||||||
* @return {String} Text with padding removed
|
* @return {String} Text with padding removed
|
||||||
*/
|
*/
|
||||||
function decode(msg) {
|
function decode(msg) {
|
||||||
var len = msg.length;
|
const len = msg.length;
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
var c = msg.charCodeAt(len - 1);
|
const c = msg.charCodeAt(len - 1);
|
||||||
if (c >= 1 && c <= 8) {
|
if (c >= 1 && c <= 8) {
|
||||||
return msg.substr(0, len - c);
|
const provided = msg.substr(len - c);
|
||||||
|
const computed = String.fromCharCode(c).repeat(c);
|
||||||
|
if (provided === computed) {
|
||||||
|
return msg.substr(0, len - c);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new Error('Invalid padding');
|
throw new Error('Invalid padding');
|
||||||
|
|
Loading…
Reference in New Issue
Block a user