diff --git a/lib/thirty-two/thirty-two.js b/lib/thirty-two/thirty-two.js index b86ab1c..f884377 100644 --- a/lib/thirty-two/thirty-two.js +++ b/lib/thirty-two/thirty-two.js @@ -1,4 +1,4 @@ -/* +/* Copyright (c) 2011, Chris Umbel Permission is hereby granted, free of charge, to any person obtaining a copy @@ -17,7 +17,7 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. +THE SOFTWARE. */ 'use strict'; @@ -37,7 +37,7 @@ var byteTable = [ function quintetCount(buff) { var quintets = Math.floor(buff.length / 5); - return buff.length % 5 == 0 ? quintets: quintets + 1; + return buff.length % 5 === 0 ? quintets: quintets + 1; } exports.encode = function(plain) { @@ -54,7 +54,7 @@ exports.encode = function(plain) { faster. will have to revisit. */ while(i < plain.length) { var current = plain[i]; - + if(shiftIndex > 3) { digit = current & (0xff >> shiftIndex); shiftIndex = (shiftIndex + 5) % 8; @@ -63,10 +63,10 @@ exports.encode = function(plain) { i++; } else { digit = (current >> (8 - (shiftIndex + 5))) & 0x1f; - shiftIndex = (shiftIndex + 5) % 8; - if(shiftIndex == 0) { i++; } + shiftIndex = (shiftIndex + 5) % 8; + if(shiftIndex === 0) i++; } - + encoded[j] = charTable.charCodeAt(digit); j++; } @@ -74,7 +74,7 @@ exports.encode = function(plain) { for(i = j; i < encoded.length; i++) { encoded[i] = 0x3d; //'='.charCodeAt(0) } - + return encoded; }; @@ -87,23 +87,23 @@ exports.decode = function(encoded) { encoded = new Buffer(encoded); } var decoded = new Buffer(Math.ceil(encoded.length * 5 / 8)); - + /* byte by byte isn't as pretty as octet by octet but tests a bit - faster. will have to revisit. */ + faster. will have to revisit. */ for(var i = 0; i < encoded.length; i++) { - if(encoded[i] == 0x3d){ //'=' + if(encoded[i] === 0x3d){ //'=' break; } - + var encodedByte = encoded[i] - 0x30; - + if(encodedByte < byteTable.length) { plainDigit = byteTable[encodedByte]; - + if(shiftIndex <= 3) { shiftIndex = (shiftIndex + 5) % 8; - - if(shiftIndex == 0) { + + if(shiftIndex === 0) { plainChar |= plainDigit; decoded[plainPos] = plainChar; plainPos++; @@ -123,5 +123,6 @@ exports.decode = function(encoded) { throw new Error('Invalid input - it is not base32 encoded string'); } } + return decoded.slice(0, plainPos); };