react-native-quick-crypto 0.7.2 → 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/QuickCrypto.podspec +34 -0
- package/android/CMakeLists.txt +32 -64
- package/android/build.gradle +52 -125
- package/android/gradle.properties +3 -3
- package/android/src/main/cpp/cpp-adapter.cpp +9 -54
- package/android/src/main/java/com/margelo/nitro/quickcrypto/QuickCryptoPackage.java +38 -0
- package/ios/QuickCryptoOnLoad.mm +19 -0
- package/lib/commonjs/index.js +39 -39
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/random.js +26 -12
- package/lib/commonjs/random.js.map +1 -1
- package/lib/commonjs/{NativeQuickCrypto/rsa.js → specs/random.nitro.js} +1 -1
- package/lib/commonjs/{NativeQuickCrypto/aes.js.map → specs/random.nitro.js.map} +1 -1
- package/lib/commonjs/utils/conversion.js +22 -0
- package/lib/commonjs/utils/conversion.js.map +1 -0
- package/lib/commonjs/utils/index.js +12 -0
- package/lib/commonjs/utils/index.js.map +1 -0
- package/lib/commonjs/utils/types.js +2 -0
- package/lib/{module/NativeQuickCrypto/aes.js.map → commonjs/utils/types.js.map} +1 -1
- package/lib/module/index.js +40 -38
- package/lib/module/index.js.map +1 -1
- package/lib/module/package.json +1 -0
- package/lib/module/random.js +28 -12
- package/lib/module/random.js.map +1 -1
- package/lib/module/specs/random.nitro.js +4 -0
- package/lib/module/{NativeQuickCrypto/rsa.js.map → specs/random.nitro.js.map} +1 -1
- package/lib/module/utils/conversion.js +16 -0
- package/lib/module/utils/conversion.js.map +1 -0
- package/lib/module/utils/index.js +8 -0
- package/lib/module/utils/index.js.map +1 -0
- package/lib/module/utils/types.js +2 -0
- package/lib/{commonjs/NativeQuickCrypto/rsa.js.map → module/utils/types.js.map} +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/typescript/index.d.ts +10 -141
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/random.d.ts +4 -5
- package/lib/typescript/random.d.ts.map +1 -1
- package/lib/typescript/specs/random.nitro.d.ts +9 -0
- package/lib/typescript/specs/random.nitro.d.ts.map +1 -0
- package/lib/typescript/utils/conversion.d.ts +4 -0
- package/lib/typescript/utils/conversion.d.ts.map +1 -0
- package/lib/typescript/utils/index.d.ts +6 -0
- package/lib/typescript/utils/index.d.ts.map +1 -0
- package/lib/typescript/utils/types.d.ts +5 -0
- package/lib/typescript/utils/types.d.ts.map +1 -0
- package/package.json +58 -108
- package/src/index.ts +38 -48
- package/src/random.ts +51 -48
- package/src/specs/random.nitro.ts +14 -0
- package/src/utils/conversion.ts +16 -0
- package/src/utils/index.ts +6 -0
- package/src/utils/types.ts +15 -0
- package/LICENSE +0 -27
- package/README.md +0 -203
- package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoModule.java +0 -70
- package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoPackage.java +0 -25
- package/cpp/Cipher/MGLCipherHostObject.cpp +0 -663
- package/cpp/Cipher/MGLCipherHostObject.h +0 -90
- package/cpp/Cipher/MGLCreateCipherInstaller.cpp +0 -75
- package/cpp/Cipher/MGLCreateCipherInstaller.h +0 -22
- package/cpp/Cipher/MGLCreateDecipherInstaller.cpp +0 -77
- package/cpp/Cipher/MGLCreateDecipherInstaller.h +0 -17
- package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +0 -134
- package/cpp/Cipher/MGLGenerateKeyPairInstaller.h +0 -35
- package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +0 -63
- package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.h +0 -37
- package/cpp/Cipher/MGLPublicCipher.h +0 -120
- package/cpp/Cipher/MGLPublicCipherInstaller.h +0 -107
- package/cpp/Cipher/MGLRsa.cpp +0 -573
- package/cpp/Cipher/MGLRsa.h +0 -109
- package/cpp/HMAC/MGLHmacHostObject.cpp +0 -95
- package/cpp/HMAC/MGLHmacHostObject.h +0 -39
- package/cpp/HMAC/MGLHmacInstaller.cpp +0 -43
- package/cpp/HMAC/MGLHmacInstaller.h +0 -20
- package/cpp/Hash/MGLHashHostObject.cpp +0 -147
- package/cpp/Hash/MGLHashHostObject.h +0 -53
- package/cpp/Hash/MGLHashInstaller.cpp +0 -44
- package/cpp/Hash/MGLHashInstaller.h +0 -20
- package/cpp/JSIUtils/MGLJSIMacros.h +0 -110
- package/cpp/JSIUtils/MGLJSIUtils.h +0 -41
- package/cpp/JSIUtils/MGLSmartHostObject.cpp +0 -43
- package/cpp/JSIUtils/MGLSmartHostObject.h +0 -46
- package/cpp/JSIUtils/MGLThreadAwareHostObject.cpp +0 -24
- package/cpp/JSIUtils/MGLThreadAwareHostObject.h +0 -43
- package/cpp/JSIUtils/MGLTypedArray.cpp +0 -325
- package/cpp/JSIUtils/MGLTypedArray.h +0 -160
- package/cpp/MGLKeys.cpp +0 -1426
- package/cpp/MGLKeys.h +0 -203
- package/cpp/MGLQuickCryptoHostObject.cpp +0 -124
- package/cpp/MGLQuickCryptoHostObject.h +0 -30
- package/cpp/Random/MGLRandomHostObject.cpp +0 -96
- package/cpp/Random/MGLRandomHostObject.h +0 -27
- package/cpp/Sig/MGLSignHostObjects.cpp +0 -757
- package/cpp/Sig/MGLSignHostObjects.h +0 -128
- package/cpp/Sig/MGLSignInstaller.cpp +0 -24
- package/cpp/Sig/MGLSignInstaller.h +0 -29
- package/cpp/Sig/MGLVerifyInstaller.cpp +0 -24
- package/cpp/Sig/MGLVerifyInstaller.h +0 -22
- package/cpp/Utils/MGLDispatchQueue.cpp +0 -75
- package/cpp/Utils/MGLDispatchQueue.h +0 -55
- package/cpp/Utils/MGLUtils.cpp +0 -295
- package/cpp/Utils/MGLUtils.h +0 -358
- package/cpp/Utils/base64.h +0 -320
- package/cpp/Utils/logs.h +0 -38
- package/cpp/Utils/node.h +0 -13
- package/cpp/fastpbkdf2/MGLPbkdf2HostObject.cpp +0 -164
- package/cpp/fastpbkdf2/MGLPbkdf2HostObject.h +0 -29
- package/cpp/fastpbkdf2/fastpbkdf2.c +0 -352
- package/cpp/fastpbkdf2/fastpbkdf2.h +0 -68
- package/cpp/webcrypto/MGLWebCrypto.cpp +0 -125
- package/cpp/webcrypto/MGLWebCrypto.h +0 -32
- package/cpp/webcrypto/crypto_aes.cpp +0 -516
- package/cpp/webcrypto/crypto_aes.h +0 -79
- package/cpp/webcrypto/crypto_ec.cpp +0 -424
- package/cpp/webcrypto/crypto_ec.h +0 -78
- package/cpp/webcrypto/crypto_keygen.cpp +0 -86
- package/cpp/webcrypto/crypto_keygen.h +0 -38
- package/ios/QuickCrypto.xcodeproj/project.pbxproj +0 -274
- package/ios/QuickCrypto.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
- package/ios/QuickCrypto.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
- package/ios/QuickCrypto.xcodeproj/project.xcworkspace/xcuserdata/brad.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
- package/ios/QuickCrypto.xcodeproj/xcuserdata/brad.xcuserdatad/xcschemes/xcschememanagement.plist +0 -14
- package/ios/QuickCryptoModule.h +0 -5
- package/ios/QuickCryptoModule.mm +0 -42
- package/lib/commonjs/Cipher.js +0 -502
- package/lib/commonjs/Cipher.js.map +0 -1
- package/lib/commonjs/Hash.js +0 -99
- package/lib/commonjs/Hash.js.map +0 -1
- package/lib/commonjs/Hashnames.js +0 -87
- package/lib/commonjs/Hashnames.js.map +0 -1
- package/lib/commonjs/Hmac.js +0 -79
- package/lib/commonjs/Hmac.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/Cipher.js +0 -30
- package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js +0 -52
- package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/aes.js +0 -6
- package/lib/commonjs/NativeQuickCrypto/hash.js +0 -2
- package/lib/commonjs/NativeQuickCrypto/hash.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/hmac.js +0 -2
- package/lib/commonjs/NativeQuickCrypto/hmac.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/keygen.js +0 -6
- package/lib/commonjs/NativeQuickCrypto/keygen.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/pbkdf2.js +0 -2
- package/lib/commonjs/NativeQuickCrypto/pbkdf2.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/random.js +0 -2
- package/lib/commonjs/NativeQuickCrypto/random.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/sig.js +0 -19
- package/lib/commonjs/NativeQuickCrypto/sig.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/webcrypto.js +0 -6
- package/lib/commonjs/NativeQuickCrypto/webcrypto.js.map +0 -1
- package/lib/commonjs/Utils.js +0 -608
- package/lib/commonjs/Utils.js.map +0 -1
- package/lib/commonjs/aes.js +0 -281
- package/lib/commonjs/aes.js.map +0 -1
- package/lib/commonjs/constants.js +0 -85
- package/lib/commonjs/constants.js.map +0 -1
- package/lib/commonjs/ec.js +0 -276
- package/lib/commonjs/ec.js.map +0 -1
- package/lib/commonjs/keygen.js +0 -56
- package/lib/commonjs/keygen.js.map +0 -1
- package/lib/commonjs/keys.js +0 -492
- package/lib/commonjs/keys.js.map +0 -1
- package/lib/commonjs/pbkdf2.js +0 -90
- package/lib/commonjs/pbkdf2.js.map +0 -1
- package/lib/commonjs/rsa.js +0 -248
- package/lib/commonjs/rsa.js.map +0 -1
- package/lib/commonjs/sig.js +0 -129
- package/lib/commonjs/sig.js.map +0 -1
- package/lib/commonjs/subtle.js +0 -448
- package/lib/commonjs/subtle.js.map +0 -1
- package/lib/commonjs/webcrypto.js +0 -14
- package/lib/commonjs/webcrypto.js.map +0 -1
- package/lib/module/Cipher.js +0 -489
- package/lib/module/Cipher.js.map +0 -1
- package/lib/module/Hash.js +0 -91
- package/lib/module/Hash.js.map +0 -1
- package/lib/module/Hashnames.js +0 -83
- package/lib/module/Hashnames.js.map +0 -1
- package/lib/module/Hmac.js +0 -72
- package/lib/module/Hmac.js.map +0 -1
- package/lib/module/NativeQuickCrypto/Cipher.js +0 -24
- package/lib/module/NativeQuickCrypto/Cipher.js.map +0 -1
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js +0 -47
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +0 -1
- package/lib/module/NativeQuickCrypto/aes.js +0 -2
- package/lib/module/NativeQuickCrypto/hash.js +0 -2
- package/lib/module/NativeQuickCrypto/hash.js.map +0 -1
- package/lib/module/NativeQuickCrypto/hmac.js +0 -2
- package/lib/module/NativeQuickCrypto/hmac.js.map +0 -1
- package/lib/module/NativeQuickCrypto/keygen.js +0 -2
- package/lib/module/NativeQuickCrypto/keygen.js.map +0 -1
- package/lib/module/NativeQuickCrypto/pbkdf2.js +0 -2
- package/lib/module/NativeQuickCrypto/pbkdf2.js.map +0 -1
- package/lib/module/NativeQuickCrypto/random.js +0 -2
- package/lib/module/NativeQuickCrypto/random.js.map +0 -1
- package/lib/module/NativeQuickCrypto/rsa.js +0 -2
- package/lib/module/NativeQuickCrypto/sig.js +0 -15
- package/lib/module/NativeQuickCrypto/sig.js.map +0 -1
- package/lib/module/NativeQuickCrypto/webcrypto.js +0 -2
- package/lib/module/NativeQuickCrypto/webcrypto.js.map +0 -1
- package/lib/module/Utils.js +0 -537
- package/lib/module/Utils.js.map +0 -1
- package/lib/module/aes.js +0 -272
- package/lib/module/aes.js.map +0 -1
- package/lib/module/constants.js +0 -79
- package/lib/module/constants.js.map +0 -1
- package/lib/module/ec.js +0 -265
- package/lib/module/ec.js.map +0 -1
- package/lib/module/keygen.js +0 -47
- package/lib/module/keygen.js.map +0 -1
- package/lib/module/keys.js +0 -475
- package/lib/module/keys.js.map +0 -1
- package/lib/module/pbkdf2.js +0 -82
- package/lib/module/pbkdf2.js.map +0 -1
- package/lib/module/rsa.js +0 -240
- package/lib/module/rsa.js.map +0 -1
- package/lib/module/sig.js +0 -122
- package/lib/module/sig.js.map +0 -1
- package/lib/module/subtle.js +0 -441
- package/lib/module/subtle.js.map +0 -1
- package/lib/module/webcrypto.js +0 -8
- package/lib/module/webcrypto.js.map +0 -1
- package/lib/typescript/Cipher.d.ts +0 -81
- package/lib/typescript/Cipher.d.ts.map +0 -1
- package/lib/typescript/Hash.d.ts +0 -44
- package/lib/typescript/Hash.d.ts.map +0 -1
- package/lib/typescript/Hashnames.d.ts +0 -11
- package/lib/typescript/Hashnames.d.ts.map +0 -1
- package/lib/typescript/Hmac.d.ts +0 -37
- package/lib/typescript/Hmac.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +0 -44
- package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +0 -33
- package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/aes.d.ts +0 -5
- package/lib/typescript/NativeQuickCrypto/aes.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/hash.d.ts +0 -7
- package/lib/typescript/NativeQuickCrypto/hash.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/hmac.d.ts +0 -6
- package/lib/typescript/NativeQuickCrypto/hmac.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/keygen.d.ts +0 -4
- package/lib/typescript/NativeQuickCrypto/keygen.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts +0 -5
- package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/random.d.ts +0 -5
- package/lib/typescript/NativeQuickCrypto/random.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/rsa.d.ts +0 -5
- package/lib/typescript/NativeQuickCrypto/rsa.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/sig.d.ts +0 -23
- package/lib/typescript/NativeQuickCrypto/sig.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts +0 -39
- package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +0 -1
- package/lib/typescript/Utils.d.ts +0 -48
- package/lib/typescript/Utils.d.ts.map +0 -1
- package/lib/typescript/aes.d.ts +0 -22
- package/lib/typescript/aes.d.ts.map +0 -1
- package/lib/typescript/constants.d.ts +0 -76
- package/lib/typescript/constants.d.ts.map +0 -1
- package/lib/typescript/ec.d.ts +0 -7
- package/lib/typescript/ec.d.ts.map +0 -1
- package/lib/typescript/keygen.d.ts +0 -6
- package/lib/typescript/keygen.d.ts.map +0 -1
- package/lib/typescript/keys.d.ts +0 -206
- package/lib/typescript/keys.d.ts.map +0 -1
- package/lib/typescript/pbkdf2.d.ts +0 -12
- package/lib/typescript/pbkdf2.d.ts.map +0 -1
- package/lib/typescript/rsa.d.ts +0 -12
- package/lib/typescript/rsa.d.ts.map +0 -1
- package/lib/typescript/sig.d.ts +0 -21
- package/lib/typescript/sig.d.ts.map +0 -1
- package/lib/typescript/subtle.d.ts +0 -15
- package/lib/typescript/subtle.d.ts.map +0 -1
- package/lib/typescript/webcrypto.d.ts +0 -9
- package/lib/typescript/webcrypto.d.ts.map +0 -1
- package/react-native-quick-crypto.podspec +0 -40
- package/src/Cipher.ts +0 -832
- package/src/Hash.ts +0 -132
- package/src/Hashnames.ts +0 -93
- package/src/Hmac.ts +0 -106
- package/src/NativeQuickCrypto/Cipher.ts +0 -102
- package/src/NativeQuickCrypto/NativeQuickCrypto.ts +0 -102
- package/src/NativeQuickCrypto/aes.ts +0 -14
- package/src/NativeQuickCrypto/hash.ts +0 -10
- package/src/NativeQuickCrypto/hmac.ts +0 -9
- package/src/NativeQuickCrypto/keygen.ts +0 -7
- package/src/NativeQuickCrypto/pbkdf2.ts +0 -16
- package/src/NativeQuickCrypto/random.ts +0 -12
- package/src/NativeQuickCrypto/rsa.ts +0 -12
- package/src/NativeQuickCrypto/sig.ts +0 -44
- package/src/NativeQuickCrypto/webcrypto.ts +0 -72
- package/src/Utils.ts +0 -777
- package/src/aes.ts +0 -402
- package/src/constants.ts +0 -79
- package/src/ec.ts +0 -375
- package/src/keygen.ts +0 -80
- package/src/keys.ts +0 -787
- package/src/pbkdf2.ts +0 -169
- package/src/rsa.ts +0 -370
- package/src/sig.ts +0 -164
- package/src/subtle.ts +0 -639
- package/src/webcrypto.ts +0 -8
- /package/android/src/main/{AndroidManifestNew.xml → AndroidManifest.xml} +0 -0
package/cpp/Utils/MGLUtils.h
DELETED
|
@@ -1,358 +0,0 @@
|
|
|
1
|
-
#ifndef MGLUtils_h
|
|
2
|
-
#define MGLUtils_h
|
|
3
|
-
|
|
4
|
-
#include <openssl/dsa.h>
|
|
5
|
-
#include <openssl/ec.h>
|
|
6
|
-
#include <openssl/err.h>
|
|
7
|
-
#include <openssl/evp.h>
|
|
8
|
-
#include <openssl/kdf.h>
|
|
9
|
-
#include <openssl/rand.h>
|
|
10
|
-
#include <openssl/rsa.h>
|
|
11
|
-
#include <openssl/ssl.h>
|
|
12
|
-
#ifndef OPENSSL_NO_ENGINE
|
|
13
|
-
#include <openssl/engine.h>
|
|
14
|
-
#endif // !OPENSSL_NO_ENGINE
|
|
15
|
-
|
|
16
|
-
#include <jsi/jsi.h>
|
|
17
|
-
#include <memory>
|
|
18
|
-
#include <optional>
|
|
19
|
-
#include <string>
|
|
20
|
-
#include <utility>
|
|
21
|
-
#include <vector>
|
|
22
|
-
#include <variant>
|
|
23
|
-
|
|
24
|
-
#ifdef ANDROID
|
|
25
|
-
#include "Utils/node.h"
|
|
26
|
-
#else
|
|
27
|
-
#include "node.h"
|
|
28
|
-
#endif
|
|
29
|
-
|
|
30
|
-
namespace margelo {
|
|
31
|
-
|
|
32
|
-
namespace jsi = facebook::jsi;
|
|
33
|
-
|
|
34
|
-
template <typename T, void (*function)(T*)>
|
|
35
|
-
struct FunctionDeleter {
|
|
36
|
-
void operator()(T* pointer) const { function(pointer); }
|
|
37
|
-
typedef std::unique_ptr<T, FunctionDeleter> Pointer;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
template <typename T, void (*function)(T*)>
|
|
41
|
-
using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer;
|
|
42
|
-
using X509Pointer = DeleteFnPtr<X509, X509_free>;
|
|
43
|
-
using BIOPointer = DeleteFnPtr<BIO, BIO_free_all>;
|
|
44
|
-
using PKCS8Pointer = DeleteFnPtr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free>;
|
|
45
|
-
using EVPKeyCtxPointer = DeleteFnPtr<EVP_PKEY_CTX, EVP_PKEY_CTX_free>;
|
|
46
|
-
using EVPKeyPointer = DeleteFnPtr<EVP_PKEY, EVP_PKEY_free>;
|
|
47
|
-
using BignumPointer = DeleteFnPtr<BIGNUM, BN_free>;
|
|
48
|
-
using RsaPointer = DeleteFnPtr<RSA, RSA_free>;
|
|
49
|
-
using EVPMDPointer = DeleteFnPtr<EVP_MD_CTX, EVP_MD_CTX_free>;
|
|
50
|
-
using ECDSASigPointer = DeleteFnPtr<ECDSA_SIG, ECDSA_SIG_free>;
|
|
51
|
-
using ECKeyPointer = DeleteFnPtr<EC_KEY, EC_KEY_free>;
|
|
52
|
-
using ECPointPointer = DeleteFnPtr<EC_POINT, EC_POINT_free>;
|
|
53
|
-
using CipherCtxPointer = DeleteFnPtr<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free>;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
#ifdef __GNUC__
|
|
57
|
-
#define MUST_USE_RESULT __attribute__((warn_unused_result))
|
|
58
|
-
#else
|
|
59
|
-
#define MUST_USE_RESULT
|
|
60
|
-
#endif
|
|
61
|
-
|
|
62
|
-
struct CSPRNGResult {
|
|
63
|
-
const bool ok;
|
|
64
|
-
MUST_USE_RESULT bool is_ok() const { return ok; }
|
|
65
|
-
MUST_USE_RESULT bool is_err() const { return !ok; }
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
// Either succeeds with exactly |length| bytes of cryptographically
|
|
69
|
-
// strong pseudo-random data, or fails. This function may block.
|
|
70
|
-
// Don't assume anything about the contents of |buffer| on error.
|
|
71
|
-
// As a special case, |length == 0| can be used to check if the CSPRNG
|
|
72
|
-
// is properly seeded without consuming entropy.
|
|
73
|
-
MUST_USE_RESULT CSPRNGResult CSPRNG(void* buffer, size_t length);
|
|
74
|
-
|
|
75
|
-
template <typename T>
|
|
76
|
-
class NonCopyableMaybe {
|
|
77
|
-
public:
|
|
78
|
-
NonCopyableMaybe() : empty_(true) {}
|
|
79
|
-
explicit NonCopyableMaybe(T&& value)
|
|
80
|
-
: empty_(false), value_(std::move(value)) {}
|
|
81
|
-
|
|
82
|
-
bool IsEmpty() const { return empty_; }
|
|
83
|
-
|
|
84
|
-
const T* get() const { return empty_ ? nullptr : &value_; }
|
|
85
|
-
|
|
86
|
-
const T* operator->() const {
|
|
87
|
-
// CHECK(!empty_);
|
|
88
|
-
return &value_;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
T&& Release() {
|
|
92
|
-
// CHECK_EQ(empty_, false);
|
|
93
|
-
empty_ = true;
|
|
94
|
-
return std::move(value_);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
private:
|
|
98
|
-
bool empty_;
|
|
99
|
-
T value_;
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
template <typename T>
|
|
103
|
-
inline T MultiplyWithOverflowCheck(T a, T b) {
|
|
104
|
-
auto ret = a * b;
|
|
105
|
-
// if (a != 0)
|
|
106
|
-
// CHECK_EQ(b, ret / a);
|
|
107
|
-
|
|
108
|
-
return ret;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
template <typename T>
|
|
112
|
-
T* MallocOpenSSL(size_t count) {
|
|
113
|
-
void* mem = OPENSSL_malloc(MultiplyWithOverflowCheck(count, sizeof(T)));
|
|
114
|
-
// CHECK_IMPLIES(mem == nullptr, count == 0);
|
|
115
|
-
return static_cast<T*>(mem);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// A helper class representing a read-only byte array. When deallocated, its
|
|
119
|
-
// contents are zeroed.
|
|
120
|
-
class ByteSource {
|
|
121
|
-
public:
|
|
122
|
-
class Builder {
|
|
123
|
-
public:
|
|
124
|
-
// Allocates memory using OpenSSL's memory allocator.
|
|
125
|
-
explicit Builder(size_t size)
|
|
126
|
-
: data_(MallocOpenSSL<char>(size)), size_(size) {}
|
|
127
|
-
|
|
128
|
-
Builder(Builder&& other) = delete;
|
|
129
|
-
Builder& operator=(Builder&& other) = delete;
|
|
130
|
-
Builder(const Builder&) = delete;
|
|
131
|
-
Builder& operator=(const Builder&) = delete;
|
|
132
|
-
|
|
133
|
-
~Builder() { OPENSSL_clear_free(data_, size_); }
|
|
134
|
-
|
|
135
|
-
// Returns the underlying non-const pointer.
|
|
136
|
-
template <typename T>
|
|
137
|
-
T* data() {
|
|
138
|
-
return reinterpret_cast<T*>(data_);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Returns the (allocated) size in bytes.
|
|
142
|
-
size_t size() const { return size_; }
|
|
143
|
-
|
|
144
|
-
// Finalizes the Builder and returns a read-only view that is optionally
|
|
145
|
-
// truncated.
|
|
146
|
-
ByteSource release(std::optional<size_t> resize = std::nullopt) && {
|
|
147
|
-
if (resize) {
|
|
148
|
-
// CHECK_LE(*resize, size_);
|
|
149
|
-
if (*resize == 0) {
|
|
150
|
-
OPENSSL_clear_free(data_, size_);
|
|
151
|
-
data_ = nullptr;
|
|
152
|
-
}
|
|
153
|
-
size_ = *resize;
|
|
154
|
-
}
|
|
155
|
-
ByteSource out = ByteSource::Allocated(data_, size_);
|
|
156
|
-
data_ = nullptr;
|
|
157
|
-
size_ = 0;
|
|
158
|
-
return out;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
private:
|
|
162
|
-
void* data_;
|
|
163
|
-
size_t size_;
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
ByteSource() = default;
|
|
167
|
-
ByteSource(ByteSource&& other) noexcept;
|
|
168
|
-
~ByteSource();
|
|
169
|
-
|
|
170
|
-
ByteSource& operator=(ByteSource&& other) noexcept;
|
|
171
|
-
|
|
172
|
-
ByteSource(const ByteSource&) = delete;
|
|
173
|
-
ByteSource& operator=(const ByteSource&) = delete;
|
|
174
|
-
|
|
175
|
-
template <typename T = void>
|
|
176
|
-
const T* data() const {
|
|
177
|
-
return reinterpret_cast<const T*>(data_);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
size_t size() const { return size_; }
|
|
181
|
-
|
|
182
|
-
operator bool() const { return data_ != nullptr; }
|
|
183
|
-
|
|
184
|
-
inline BignumPointer ToBN() const {
|
|
185
|
-
return BignumPointer(BN_bin2bn(data<unsigned char>(), (int)size(), nullptr));
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
inline std::string ToString() const {
|
|
189
|
-
std::vector<uint8_t> buf(size_);
|
|
190
|
-
std::memcpy(&buf[0], data_, size_);
|
|
191
|
-
std::string ret(buf.begin(), buf.end());
|
|
192
|
-
return ret;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// Creates a v8::BackingStore that takes over responsibility for
|
|
196
|
-
// any allocated data. The ByteSource will be reset with size = 0
|
|
197
|
-
// after being called.
|
|
198
|
-
// std::unique_ptr<v8::BackingStore> ReleaseToBackingStore();
|
|
199
|
-
//
|
|
200
|
-
// v8::Local<v8::ArrayBuffer> ToArrayBuffer(Environment* env);
|
|
201
|
-
//
|
|
202
|
-
// v8::MaybeLocal<v8::Uint8Array> ToBuffer(Environment* env);
|
|
203
|
-
|
|
204
|
-
static ByteSource Allocated(void* data, size_t size);
|
|
205
|
-
static ByteSource Foreign(const void* data, size_t size);
|
|
206
|
-
|
|
207
|
-
static ByteSource FromEncodedString(jsi::Runtime &rt,
|
|
208
|
-
std::string value,
|
|
209
|
-
enum encoding enc = BASE64);
|
|
210
|
-
|
|
211
|
-
static ByteSource FromStringOrBuffer(jsi::Runtime& runtime,
|
|
212
|
-
const jsi::Value& value);
|
|
213
|
-
|
|
214
|
-
static ByteSource FromString(std::string str, bool ntc = false);
|
|
215
|
-
|
|
216
|
-
static ByteSource FromBuffer(jsi::Runtime& runtime,
|
|
217
|
-
const jsi::ArrayBuffer& buffer,
|
|
218
|
-
bool ntc = false);
|
|
219
|
-
|
|
220
|
-
static ByteSource FromBIO(const BIOPointer& bio);
|
|
221
|
-
|
|
222
|
-
static ByteSource FromBN(const BIGNUM* bn, size_t size);
|
|
223
|
-
|
|
224
|
-
// static ByteSource NullTerminatedCopy(Environment* env,
|
|
225
|
-
// v8::Local<v8::Value> value);
|
|
226
|
-
//
|
|
227
|
-
// static ByteSource FromSymmetricKeyObjectHandle(v8::Local<v8::Value>
|
|
228
|
-
// handle);
|
|
229
|
-
|
|
230
|
-
// static ByteSource FromSecretKeyBytes(
|
|
231
|
-
// Environment* env,
|
|
232
|
-
// v8::Local<v8::Value> value);
|
|
233
|
-
|
|
234
|
-
private:
|
|
235
|
-
const void* data_ = nullptr;
|
|
236
|
-
void* allocated_data_ = nullptr;
|
|
237
|
-
size_t size_ = 0;
|
|
238
|
-
|
|
239
|
-
ByteSource(const void* data, void* allocated_data, size_t size)
|
|
240
|
-
: data_(data), allocated_data_(allocated_data), size_(size) {}
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
ByteSource ArrayBufferToByteSource(jsi::Runtime& runtime,
|
|
244
|
-
const jsi::ArrayBuffer& buffer);
|
|
245
|
-
|
|
246
|
-
ByteSource ArrayBufferToNTCByteSource(jsi::Runtime& runtime,
|
|
247
|
-
const jsi::ArrayBuffer& buffer);
|
|
248
|
-
|
|
249
|
-
// Originally part of the ArrayBufferContentOrView class
|
|
250
|
-
inline ByteSource ToNullTerminatedByteSource(jsi::Runtime& runtime,
|
|
251
|
-
jsi::ArrayBuffer& buffer) {
|
|
252
|
-
if (buffer.size(runtime) == 0) return ByteSource();
|
|
253
|
-
char* buf = MallocOpenSSL<char>(buffer.size(runtime) + 1);
|
|
254
|
-
// CHECK_NOT_NULL(buf);
|
|
255
|
-
buf[buffer.size(runtime)] = 0;
|
|
256
|
-
memcpy(buf, buffer.data(runtime), buffer.size(runtime));
|
|
257
|
-
return ByteSource::Allocated(buf, buffer.size(runtime));
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
inline int PasswordCallback(char* buf, int size, int rwflag, void* u) {
|
|
261
|
-
const ByteSource* passphrase = *static_cast<const ByteSource**>(u);
|
|
262
|
-
if (passphrase != nullptr) {
|
|
263
|
-
size_t buflen = static_cast<size_t>(size);
|
|
264
|
-
size_t len = passphrase->size();
|
|
265
|
-
if (buflen < len) return -1;
|
|
266
|
-
memcpy(buf, passphrase->data(), len);
|
|
267
|
-
return (int)len;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
return -1;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
inline void CheckEntropy() {
|
|
274
|
-
for (;;) {
|
|
275
|
-
int status = RAND_status();
|
|
276
|
-
// CHECK_GE(status, 0); // Cannot fail.
|
|
277
|
-
if (status != 0) break;
|
|
278
|
-
|
|
279
|
-
// Give up, RAND_poll() not supported.
|
|
280
|
-
if (RAND_poll() == 0) break;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
std::string StringBytesWrite(jsi::Runtime &rt,
|
|
285
|
-
const std::string val,
|
|
286
|
-
enum encoding encoding);
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
inline jsi::Value toJSI(jsi::Runtime& rt, std::string value) {
|
|
290
|
-
return jsi::String::createFromUtf8(rt, value);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
inline jsi::Value toJSI(jsi::Runtime& rt, ByteSource value) {
|
|
294
|
-
jsi::Function array_buffer_ctor =
|
|
295
|
-
rt.global().getPropertyAsFunction(rt, "ArrayBuffer");
|
|
296
|
-
jsi::Object o = array_buffer_ctor.callAsConstructor(rt, (int)value.size())
|
|
297
|
-
.getObject(rt);
|
|
298
|
-
jsi::ArrayBuffer buf = o.getArrayBuffer(rt);
|
|
299
|
-
// You cannot share raw memory between native and JS
|
|
300
|
-
// always copy the data
|
|
301
|
-
// see https://github.com/facebook/hermes/pull/419 and
|
|
302
|
-
// https://github.com/facebook/hermes/issues/564.
|
|
303
|
-
memcpy(buf.data(rt), value.data(), value.size());
|
|
304
|
-
return o;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
ByteSource GetByteSourceFromJS(jsi::Runtime &rt,
|
|
308
|
-
const jsi::Value &value,
|
|
309
|
-
std::string name);
|
|
310
|
-
|
|
311
|
-
std::string EncodeBignum(const BIGNUM* bn,
|
|
312
|
-
size_t size,
|
|
313
|
-
bool url = false);
|
|
314
|
-
|
|
315
|
-
std::string EncodeBase64(const std::string data, bool url = false);
|
|
316
|
-
std::string DecodeBase64(const std::string &in, bool remove_linebreaks = false);
|
|
317
|
-
|
|
318
|
-
bool SetRsaOaepLabel(const EVPKeyCtxPointer& ctx, const ByteSource& label);
|
|
319
|
-
|
|
320
|
-
// TODO: until shared, keep in sync with JS side (src/NativeQuickCrypto/Cipher.ts)
|
|
321
|
-
enum KeyVariant {
|
|
322
|
-
kvRSA_SSA_PKCS1_v1_5,
|
|
323
|
-
kvRSA_PSS,
|
|
324
|
-
kvRSA_OAEP,
|
|
325
|
-
kvDSA,
|
|
326
|
-
kvEC,
|
|
327
|
-
kvNID,
|
|
328
|
-
kvDH,
|
|
329
|
-
};
|
|
330
|
-
|
|
331
|
-
enum FnMode {
|
|
332
|
-
kAsync,
|
|
333
|
-
kSync,
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
enum WebCryptoKeyFormat {
|
|
337
|
-
kWebCryptoKeyFormatRaw,
|
|
338
|
-
kWebCryptoKeyFormatPKCS8,
|
|
339
|
-
kWebCryptoKeyFormatSPKI,
|
|
340
|
-
kWebCryptoKeyFormatJWK
|
|
341
|
-
};
|
|
342
|
-
|
|
343
|
-
enum WebCryptoCipherMode {
|
|
344
|
-
kEncrypt,
|
|
345
|
-
kDecrypt,
|
|
346
|
-
// kWrapKey,
|
|
347
|
-
// kUnwrapKey,
|
|
348
|
-
};
|
|
349
|
-
|
|
350
|
-
enum class WebCryptoCipherStatus {
|
|
351
|
-
OK,
|
|
352
|
-
INVALID_KEY_TYPE,
|
|
353
|
-
FAILED
|
|
354
|
-
};
|
|
355
|
-
|
|
356
|
-
} // namespace margelo
|
|
357
|
-
|
|
358
|
-
#endif /* MGLUtils_h */
|
package/cpp/Utils/base64.h
DELETED
|
@@ -1,320 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
base64.h
|
|
3
|
-
|
|
4
|
-
base64 encoding and decoding with C++.
|
|
5
|
-
More information at
|
|
6
|
-
https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp
|
|
7
|
-
|
|
8
|
-
Version: 2.rc.09 (release candidate)
|
|
9
|
-
|
|
10
|
-
Copyright (C) 2004-2017, 2020-2022 René Nyffenegger
|
|
11
|
-
|
|
12
|
-
This source code is provided 'as-is', without any express or implied
|
|
13
|
-
warranty. In no event will the author be held liable for any damages
|
|
14
|
-
arising from the use of this software.
|
|
15
|
-
|
|
16
|
-
Permission is granted to anyone to use this software for any purpose,
|
|
17
|
-
including commercial applications, and to alter it and redistribute it
|
|
18
|
-
freely, subject to the following restrictions:
|
|
19
|
-
|
|
20
|
-
1. The origin of this source code must not be misrepresented; you must not
|
|
21
|
-
claim that you wrote the original source code. If you use this source code
|
|
22
|
-
in a product, an acknowledgment in the product documentation would be
|
|
23
|
-
appreciated but is not required.
|
|
24
|
-
|
|
25
|
-
2. Altered source versions must be plainly marked as such, and must not be
|
|
26
|
-
misrepresented as being the original source code.
|
|
27
|
-
|
|
28
|
-
3. This notice may not be removed or altered from any source distribution.
|
|
29
|
-
|
|
30
|
-
René Nyffenegger rene.nyffenegger@adp-gmbh.ch
|
|
31
|
-
*/
|
|
32
|
-
/**
|
|
33
|
-
* Copyright (C) 2023 Kevin Heifner
|
|
34
|
-
*
|
|
35
|
-
* Modified to be header only.
|
|
36
|
-
* Templated for std::string, std::string_view, std::vector<char> and other char containers.
|
|
37
|
-
*/
|
|
38
|
-
|
|
39
|
-
#pragma once
|
|
40
|
-
|
|
41
|
-
#include <algorithm>
|
|
42
|
-
#include <string>
|
|
43
|
-
#include <string_view>
|
|
44
|
-
#include <stdexcept>
|
|
45
|
-
|
|
46
|
-
// Interface:
|
|
47
|
-
// Defaults allow for use:
|
|
48
|
-
// std::string s = "foobar";
|
|
49
|
-
// std::string encoded = base64_encode(s);
|
|
50
|
-
// std::string_view sv = "foobar";
|
|
51
|
-
// std::string encoded = base64_encode(sv);
|
|
52
|
-
// std::vector<char> vc = {'f', 'o', 'o'};
|
|
53
|
-
// std::string encoded = base64_encode(vc);
|
|
54
|
-
//
|
|
55
|
-
// Also allows for user provided char containers and specified return types:
|
|
56
|
-
// std::string s = "foobar";
|
|
57
|
-
// std::vector<char> encoded = base64_encode<std::vector<char>>(s);
|
|
58
|
-
|
|
59
|
-
template <typename RetString = std::string, typename String = std::string>
|
|
60
|
-
RetString base64_encode(const String& s, bool url = false);
|
|
61
|
-
|
|
62
|
-
template <typename RetString = std::string, typename String = std::string>
|
|
63
|
-
RetString base64_encode_pem(const String& s);
|
|
64
|
-
|
|
65
|
-
template <typename RetString = std::string, typename String = std::string>
|
|
66
|
-
RetString base64_encode_mime(const String& s);
|
|
67
|
-
|
|
68
|
-
template <typename RetString = std::string, typename String = std::string>
|
|
69
|
-
RetString base64_decode(const String& s, bool remove_linebreaks = false);
|
|
70
|
-
|
|
71
|
-
template <typename RetString = std::string>
|
|
72
|
-
RetString base64_encode(const unsigned char* s, size_t len, bool url = false);
|
|
73
|
-
|
|
74
|
-
namespace detail {
|
|
75
|
-
//
|
|
76
|
-
// Depending on the url parameter in base64_chars, one of
|
|
77
|
-
// two sets of base64 characters needs to be chosen.
|
|
78
|
-
// They differ in their last two characters.
|
|
79
|
-
//
|
|
80
|
-
constexpr const char* to_base64_chars[2] = {
|
|
81
|
-
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
82
|
-
"abcdefghijklmnopqrstuvwxyz"
|
|
83
|
-
"0123456789"
|
|
84
|
-
"+/",
|
|
85
|
-
|
|
86
|
-
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
87
|
-
"abcdefghijklmnopqrstuvwxyz"
|
|
88
|
-
"0123456789"
|
|
89
|
-
"-_"};
|
|
90
|
-
|
|
91
|
-
constexpr unsigned char from_base64_chars[256] = {
|
|
92
|
-
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
|
93
|
-
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
|
94
|
-
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 62, 64, 63,
|
|
95
|
-
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
|
|
96
|
-
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
|
97
|
-
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 63,
|
|
98
|
-
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
|
99
|
-
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
|
|
100
|
-
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
|
101
|
-
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
|
102
|
-
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
|
103
|
-
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
|
104
|
-
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
|
105
|
-
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
|
106
|
-
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
|
107
|
-
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
inline unsigned int pos_of_char(const unsigned char chr) {
|
|
111
|
-
//
|
|
112
|
-
// Return the position of chr within base64_encode()
|
|
113
|
-
//
|
|
114
|
-
|
|
115
|
-
if (from_base64_chars[chr] != 64) return from_base64_chars[chr];
|
|
116
|
-
|
|
117
|
-
//
|
|
118
|
-
// 2020-10-23: Throw std::exception rather than const char*
|
|
119
|
-
//(Pablo Martin-Gomez, https://github.com/Bouska)
|
|
120
|
-
//
|
|
121
|
-
throw std::runtime_error("Input is not valid base64-encoded data.");
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
template <typename RetString, typename String>
|
|
125
|
-
inline RetString insert_linebreaks(const String& str, size_t distance) {
|
|
126
|
-
//
|
|
127
|
-
// Provided by https://github.com/JomaCorpFX, adapted by Rene & Kevin
|
|
128
|
-
//
|
|
129
|
-
if (!str.size()) {
|
|
130
|
-
return RetString{};
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (distance < str.size()) {
|
|
134
|
-
size_t pos = distance;
|
|
135
|
-
String s{str};
|
|
136
|
-
while (pos < s.size()) {
|
|
137
|
-
s.insert(pos, "\n");
|
|
138
|
-
pos += distance + 1;
|
|
139
|
-
}
|
|
140
|
-
return s;
|
|
141
|
-
} else {
|
|
142
|
-
return str;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
template <typename RetString, typename String, unsigned int line_length>
|
|
147
|
-
inline RetString encode_with_line_breaks(String s) {
|
|
148
|
-
return insert_linebreaks<RetString, String>(base64_encode(s, false), line_length);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
template <typename RetString, typename String>
|
|
152
|
-
inline RetString encode_pem(String s) {
|
|
153
|
-
return encode_with_line_breaks<RetString, String, 64>(s);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
template <typename RetString, typename String>
|
|
157
|
-
inline RetString encode_mime(String s) {
|
|
158
|
-
return encode_with_line_breaks<RetString, String, 76>(s);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
template <typename RetString, typename String>
|
|
162
|
-
inline RetString encode(String s, bool url) {
|
|
163
|
-
return base64_encode<RetString>(reinterpret_cast<const unsigned char*>(s.data()), s.size(), url);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
} // namespace detail
|
|
167
|
-
|
|
168
|
-
template <typename RetString>
|
|
169
|
-
inline RetString base64_encode(const unsigned char* bytes_to_encode, size_t in_len, bool url) {
|
|
170
|
-
size_t len_encoded = (in_len + 2) / 3 * 4;
|
|
171
|
-
|
|
172
|
-
unsigned char trailing_char = url ? '.' : '=';
|
|
173
|
-
|
|
174
|
-
//
|
|
175
|
-
// Choose set of base64 characters. They differ
|
|
176
|
-
// for the last two positions, depending on the url
|
|
177
|
-
// parameter.
|
|
178
|
-
// A bool (as is the parameter url) is guaranteed
|
|
179
|
-
// to evaluate to either 0 or 1 in C++ therefore,
|
|
180
|
-
// the correct character set is chosen by subscripting
|
|
181
|
-
// base64_chars with url.
|
|
182
|
-
//
|
|
183
|
-
const char *base64_chars_ = detail::to_base64_chars[url];
|
|
184
|
-
|
|
185
|
-
RetString ret;
|
|
186
|
-
ret.reserve(len_encoded);
|
|
187
|
-
|
|
188
|
-
unsigned int pos = 0;
|
|
189
|
-
|
|
190
|
-
while (pos < in_len) {
|
|
191
|
-
ret.push_back(base64_chars_[(bytes_to_encode[pos + 0] & 0xfc) >> 2]);
|
|
192
|
-
|
|
193
|
-
if (pos + 1 < in_len) {
|
|
194
|
-
ret.push_back(base64_chars_[((bytes_to_encode[pos + 0] & 0x03) << 4) +
|
|
195
|
-
((bytes_to_encode[pos + 1] & 0xf0) >> 4)]);
|
|
196
|
-
|
|
197
|
-
if (pos + 2 < in_len) {
|
|
198
|
-
ret.push_back(base64_chars_[((bytes_to_encode[pos + 1] & 0x0f) << 2) +
|
|
199
|
-
((bytes_to_encode[pos + 2] & 0xc0) >> 6)]);
|
|
200
|
-
ret.push_back(base64_chars_[bytes_to_encode[pos + 2] & 0x3f]);
|
|
201
|
-
} else {
|
|
202
|
-
ret.push_back(base64_chars_[(bytes_to_encode[pos + 1] & 0x0f) << 2]);
|
|
203
|
-
ret.push_back(trailing_char);
|
|
204
|
-
}
|
|
205
|
-
} else {
|
|
206
|
-
ret.push_back(base64_chars_[(bytes_to_encode[pos + 0] & 0x03) << 4]);
|
|
207
|
-
ret.push_back(trailing_char);
|
|
208
|
-
ret.push_back(trailing_char);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
pos += 3;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
return ret;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
namespace detail {
|
|
218
|
-
|
|
219
|
-
template <typename RetString, typename String>
|
|
220
|
-
inline RetString decode(const String& encoded_string, bool remove_linebreaks) {
|
|
221
|
-
static_assert(!std::is_same<RetString, std::string_view>::value,
|
|
222
|
-
"RetString should not be std::string_view");
|
|
223
|
-
|
|
224
|
-
//
|
|
225
|
-
// decode(…) is templated so that it can be used with String = const std::string&
|
|
226
|
-
// or std::string_view (requires at least C++17)
|
|
227
|
-
//
|
|
228
|
-
|
|
229
|
-
if (encoded_string.empty())
|
|
230
|
-
return RetString{};
|
|
231
|
-
|
|
232
|
-
if (remove_linebreaks) {
|
|
233
|
-
String copy{encoded_string};
|
|
234
|
-
|
|
235
|
-
copy.erase(std::remove(copy.begin(), copy.end(), '\n'), copy.end());
|
|
236
|
-
|
|
237
|
-
return base64_decode<RetString, String>(copy, false);
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
size_t length_of_string = encoded_string.size();
|
|
241
|
-
size_t pos = 0;
|
|
242
|
-
|
|
243
|
-
//
|
|
244
|
-
// The approximate length (bytes) of the decoded string might be one or
|
|
245
|
-
// two bytes smaller, depending on the amount of trailing equal signs
|
|
246
|
-
// in the encoded string. This approximation is needed to reserve
|
|
247
|
-
// enough space in the string to be returned.
|
|
248
|
-
//
|
|
249
|
-
size_t approx_length_of_decoded_string = length_of_string / 4 * 3;
|
|
250
|
-
RetString ret;
|
|
251
|
-
ret.reserve(approx_length_of_decoded_string);
|
|
252
|
-
|
|
253
|
-
while (pos < length_of_string) {
|
|
254
|
-
//
|
|
255
|
-
// Iterate over encoded input string in chunks. The size of all
|
|
256
|
-
// chunks except the last one is 4 bytes.
|
|
257
|
-
//
|
|
258
|
-
// The last chunk might be padded with equal signs or dots
|
|
259
|
-
// in order to make it 4 bytes in size as well, but this
|
|
260
|
-
// is not required as per RFC 2045.
|
|
261
|
-
//
|
|
262
|
-
// All chunks except the last one produce three output bytes.
|
|
263
|
-
//
|
|
264
|
-
// The last chunk produces at least one and up to three bytes.
|
|
265
|
-
//
|
|
266
|
-
|
|
267
|
-
size_t pos_of_char_1 = pos_of_char(encoded_string.at(pos + 1));
|
|
268
|
-
|
|
269
|
-
//
|
|
270
|
-
// Emit the first output byte that is produced in each chunk:
|
|
271
|
-
//
|
|
272
|
-
ret.push_back(static_cast<typename RetString::value_type>(((pos_of_char(encoded_string.at(pos + 0))) << 2) + ((pos_of_char_1 & 0x30) >> 4)));
|
|
273
|
-
|
|
274
|
-
if ((pos + 2 < length_of_string) &&
|
|
275
|
-
// Check for data that is not padded with equal signs (which is allowed by RFC 2045)
|
|
276
|
-
encoded_string.at(pos + 2) != '=' &&
|
|
277
|
-
encoded_string.at(pos + 2) != '.' ) { // accept URL-safe base 64 strings, too, so check for '.' also.
|
|
278
|
-
//
|
|
279
|
-
// Emit a chunk's second byte (which might not be produced in the last chunk).
|
|
280
|
-
//
|
|
281
|
-
unsigned int pos_of_char_2 = pos_of_char(encoded_string.at(pos + 2));
|
|
282
|
-
ret.push_back(static_cast<typename RetString::value_type>(((pos_of_char_1 & 0x0f) << 4) + ((pos_of_char_2 & 0x3c) >> 2)));
|
|
283
|
-
|
|
284
|
-
if ((pos + 3 < length_of_string) &&
|
|
285
|
-
encoded_string.at(pos + 3) != '=' &&
|
|
286
|
-
encoded_string.at(pos + 3) != '.' ) {
|
|
287
|
-
//
|
|
288
|
-
// Emit a chunk's third byte (which might not be produced in the last chunk).
|
|
289
|
-
//
|
|
290
|
-
ret.push_back(static_cast<typename RetString::value_type>(((pos_of_char_2 & 0x03) << 6) + pos_of_char(encoded_string.at(pos + 3))));
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
pos += 4;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
return ret;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
} // namespace detail
|
|
301
|
-
|
|
302
|
-
template <typename RetString, typename String>
|
|
303
|
-
inline RetString base64_decode(const String& s, bool remove_linebreaks) {
|
|
304
|
-
return detail::decode<RetString, String>(s, remove_linebreaks);
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
template <typename RetString, typename String>
|
|
308
|
-
inline RetString base64_encode(const String& s, bool url) {
|
|
309
|
-
return detail::encode<RetString, String>(s, url);
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
template <typename RetString, typename String>
|
|
313
|
-
inline RetString base64_encode_pem (const String& s) {
|
|
314
|
-
return detail::encode_pem<RetString, String>(s);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
template <typename RetString, typename String>
|
|
318
|
-
inline RetString base64_encode_mime(const String& s) {
|
|
319
|
-
return detail::encode_mime<RetString, String>(s);
|
|
320
|
-
}
|