Is it ok to remove the equal signs from a base64 string?

前端 未结 6 1056
眼角桃花
眼角桃花 2021-02-02 10:05

I have a string that I\'m encoding into base64 to conserve space. Is it a big deal if I remove the equal sign at the end? Would this significantly decrease entropy? What can I d

6条回答
  •  终归单人心
    2021-02-02 10:58

    Looking at your code:

    >>> base64.b64encode(combined.digest(), altchars="AB")
    'PeFC3irNFx8fuzwjAzAfEAup9cz6xujsf2gAIH2GdUM='
    

    The string that's being encoded in base64 is the result of a function called digest(). If your digest function is producing fixed length values (e.g. if it's calculating MD5 or SHA1 digests), then the parameter to b64encode will always be the same length.

    If the above is true, then you can strip of the trailing equals signs, because there will always be the same number of them. If you do that, simply append the same number of equals signs to the string before you decode.

    If the digest is not a fixed length, then it's not safe to trim the equals signs.

    Edit: Looks like you might be using a SHA-256 digest? The SHA-256 digest is 256 bits (or 32 bytes). 32 bytes is 10 groups of 3, plus two left over. As you'll see from the Wikipedia section on padding; that'd mean you always have one trailing equals. If it is SHA-256, then it'd be OK to strip it, so long as you remember to add it again before decoding.

提交回复
热议问题