0
$\begingroup$

Basically I need to have a base64 encoded signature that has to be 96 characters long. However, I do not know what length I need to have the signature at so when I encode it, it can be that exact size. Does anyone know how to convert it? Also, does anyone know how to convert it in base64 encoding or any byte size encoding?

$\endgroup$
1
3
$\begingroup$

However, I do not know what length I need to have the signature at so when I encode it, it can be that exact size.

Well, base64 uses 4 characters (from an alphabet of size 64) to encode 3 bytes (3 bits contain 24 bits; 24/4 = 6 bits per base64 character).

Hence, if the signature was 72 bytes long, that would translate to 72/3*4 = 96 characters you require.

Now, base64 typically has a === trailer to signify the end of the encoding; are you counting that?

$\endgroup$
4
  • $\begingroup$ Okay, thanks a lot for clearing it up poncho. $\endgroup$
    – John Chen
    Sep 14 '20 at 22:35
  • 2
    $\begingroup$ Some base64 (mostly PEM and MIME and derivatives thereof) has padding which can be 1 or 2 equalsign but never 3, and is not present if the input is an exact multiple of 3. PGP 'armor' does have a trailer with 1 equalsign followed by 4 base64 chars (exactly) encoding CRC24. $\endgroup$ Sep 15 '20 at 2:44
  • $\begingroup$ Per Base64, $b>0$ byte(s) encode to $c=4\,\left\lceil b/3\right\rceil$ characters ending in $n=2-(b+2\bmod 3)=(-b)\bmod3$ time(s) the character =. And for decoding, $c$ characters with $c$ multiple of $4$, ending in $n$ times the character = with $0\le n<3$, decode to $b=3\,c/4-n$ byte(s). $\endgroup$
    – fgrieu
    Sep 15 '20 at 5:18
  • $\begingroup$ Can Base64 encoded string end with 3 equal signs?. No! $\endgroup$
    – kelalaka
    Sep 15 '20 at 7:46
0
$\begingroup$

base64 is defined on rfc4648

As poncho mentions, it has an overhead of 33%, and thus 96 / 4 × 3 = 72 bytes needed

$\endgroup$
2
  • $\begingroup$ In this answer, it is meant bytes where there now is characters. $\endgroup$
    – fgrieu
    Sep 15 '20 at 5:19
  • $\begingroup$ Indeed. Fixed. Thanks @fgrieu $\endgroup$
    – Ángel
    Sep 15 '20 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.