1313 ******************************************************************************/
1414
1515#include < utility/SElementJWS.h>
16+ #include < utility/SElementBase64.h>
1617#include < ArduinoECCX08.h>
1718#include < utility/ASN1Utils.h>
18- #include < utility/PEMUtils.h>
19-
20- static String base64urlEncode (const byte in[], unsigned int length)
21- {
22- static const char * CODES = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=" ;
23-
24- int b;
25- String out;
26-
27- int reserveLength = 4 * ((length + 2 ) / 3 );
28- out.reserve (reserveLength);
29-
30- for (unsigned int i = 0 ; i < length; i += 3 ) {
31- b = (in[i] & 0xFC ) >> 2 ;
32- out += CODES [b];
33-
34- b = (in[i] & 0x03 ) << 4 ;
35- if (i + 1 < length) {
36- b |= (in[i + 1 ] & 0xF0 ) >> 4 ;
37- out += CODES [b];
38- b = (in[i + 1 ] & 0x0F ) << 2 ;
39- if (i + 2 < length) {
40- b |= (in[i + 2 ] & 0xC0 ) >> 6 ;
41- out += CODES [b];
42- b = in[i + 2 ] & 0x3F ;
43- out += CODES [b];
44- } else {
45- out += CODES [b];
46- }
47- } else {
48- out += CODES [b];
49- }
50- }
51-
52- while (out.lastIndexOf (' =' ) != -1 ) {
53- out.remove (out.length () - 1 );
54- }
55-
56- return out;
57- }
5819
5920String SElementJWS::publicKey (SecureElement & se, int slot, bool newPrivateKey)
6021{
@@ -79,7 +40,7 @@ String SElementJWS::publicKey(SecureElement & se, int slot, bool newPrivateKey)
7940
8041 ASN1Utils.appendPublicKey (publicKey, out);
8142
82- return PEMUtils. base64Encode (out, length, " -----BEGIN PUBLIC KEY-----\n " , " \n -----END PUBLIC KEY-----\n " );
43+ return b64::encode (out, length, " -----BEGIN PUBLIC KEY-----\n " , " \n -----END PUBLIC KEY-----\n " );
8344}
8445
8546String SElementJWS::sign (SecureElement & se, int slot, const char * header, const char * payload)
@@ -88,8 +49,8 @@ String SElementJWS::sign(SecureElement & se, int slot, const char* header, const
8849 return " " ;
8950 }
9051
91- String encodedHeader = base64urlEncode ((const byte*)header, strlen (header));
92- String encodedPayload = base64urlEncode ((const byte*)payload, strlen (payload));
52+ String encodedHeader = b64::urlEncode ((const byte*)header, strlen (header));
53+ String encodedPayload = b64::urlEncode ((const byte*)payload, strlen (payload));
9354
9455 String toSign;
9556 toSign.reserve (encodedHeader.length () + 1 + encodedPayload.length ());
@@ -108,7 +69,7 @@ String SElementJWS::sign(SecureElement & se, int slot, const char* header, const
10869 return " " ;
10970 }
11071
111- String encodedSignature = base64urlEncode (signature, sizeof (signature));
72+ String encodedSignature = b64::urlEncode (signature, sizeof (signature));
11273
11374 String result;
11475 result.reserve (toSign.length () + 1 + encodedSignature.length ());
0 commit comments