react-native-quick-crypto 1.0.0-beta.12 → 1.0.0-beta.13
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/android/CMakeLists.txt +2 -0
- package/cpp/ed25519/HybridEdKeyPair.cpp +32 -89
- package/cpp/ed25519/HybridEdKeyPair.hpp +24 -54
- package/cpp/hash/HybridHash.cpp +30 -60
- package/cpp/hash/HybridHash.hpp +11 -27
- package/cpp/hmac/HybridHmac.cpp +95 -0
- package/cpp/hmac/HybridHmac.hpp +31 -0
- package/cpp/pbkdf2/HybridPbkdf2.cpp +34 -55
- package/cpp/pbkdf2/HybridPbkdf2.hpp +5 -16
- package/cpp/random/HybridRandom.cpp +5 -16
- package/cpp/random/HybridRandom.hpp +5 -6
- package/cpp/utils/Utils.hpp +1 -2
- package/lib/commonjs/hash.js +41 -18
- package/lib/commonjs/hash.js.map +1 -1
- package/lib/commonjs/hmac.js +109 -0
- package/lib/commonjs/hmac.js.map +1 -0
- package/lib/commonjs/index.js +13 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/specs/hmac.nitro.js +6 -0
- package/lib/commonjs/specs/hmac.nitro.js.map +1 -0
- package/lib/module/hash.js +41 -18
- package/lib/module/hash.js.map +1 -1
- package/lib/module/hmac.js +104 -0
- package/lib/module/hmac.js.map +1 -0
- package/lib/module/index.js +3 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/specs/hmac.nitro.js +4 -0
- package/lib/module/specs/hmac.nitro.js.map +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/typescript/hash.d.ts +23 -0
- package/lib/typescript/hash.d.ts.map +1 -1
- package/lib/typescript/hmac.d.ts +67 -0
- package/lib/typescript/hmac.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +2 -0
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/specs/hmac.nitro.d.ts +10 -0
- package/lib/typescript/specs/hmac.nitro.d.ts.map +1 -0
- package/nitrogen/generated/android/QuickCrypto+autolinking.cmake +1 -0
- package/nitrogen/generated/android/QuickCryptoOnLoad.cpp +10 -0
- package/nitrogen/generated/ios/QuickCryptoAutolinking.mm +10 -0
- package/nitrogen/generated/shared/c++/HybridHmacSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridHmacSpec.hpp +66 -0
- package/package.json +1 -1
- package/src/hash.ts +51 -15
- package/src/hmac.ts +135 -0
- package/src/index.ts +3 -0
- package/src/specs/hmac.nitro.ts +7 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#include <NitroModules/ArrayBuffer.hpp>
|
|
2
|
+
#include <memory>
|
|
3
|
+
#include <openssl/evp.h>
|
|
4
|
+
#include <optional>
|
|
5
|
+
#include <string>
|
|
6
|
+
#include <vector>
|
|
7
|
+
|
|
8
|
+
#include "HybridHmacSpec.hpp"
|
|
9
|
+
|
|
10
|
+
namespace margelo::nitro::crypto {
|
|
11
|
+
|
|
12
|
+
using namespace facebook;
|
|
13
|
+
|
|
14
|
+
class HybridHmac : public HybridHmacSpec {
|
|
15
|
+
public:
|
|
16
|
+
HybridHmac() : HybridObject(TAG) {}
|
|
17
|
+
~HybridHmac();
|
|
18
|
+
|
|
19
|
+
public:
|
|
20
|
+
// Methods
|
|
21
|
+
void createHmac(const std::string& algorithm, const std::shared_ptr<ArrayBuffer>& key) override;
|
|
22
|
+
void update(const std::shared_ptr<ArrayBuffer>& data) override;
|
|
23
|
+
std::shared_ptr<ArrayBuffer> digest() override;
|
|
24
|
+
|
|
25
|
+
private:
|
|
26
|
+
// Properties
|
|
27
|
+
EVP_MAC_CTX* ctx = nullptr;
|
|
28
|
+
std::string algorithm = "";
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
} // namespace margelo::nitro::crypto
|
|
@@ -3,70 +3,49 @@
|
|
|
3
3
|
|
|
4
4
|
namespace margelo::nitro::crypto {
|
|
5
5
|
|
|
6
|
-
std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>>
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const std::shared_ptr<ArrayBuffer>& salt,
|
|
10
|
-
double iterations,
|
|
11
|
-
double keylen,
|
|
12
|
-
const std::string& digest
|
|
13
|
-
) {
|
|
6
|
+
std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>> HybridPbkdf2::pbkdf2(const std::shared_ptr<ArrayBuffer>& password,
|
|
7
|
+
const std::shared_ptr<ArrayBuffer>& salt, double iterations,
|
|
8
|
+
double keylen, const std::string& digest) {
|
|
14
9
|
// get owned NativeArrayBuffers before passing to sync function
|
|
15
10
|
auto nativePassword = ToNativeArrayBuffer(password);
|
|
16
11
|
auto nativeSalt = ToNativeArrayBuffer(salt);
|
|
17
12
|
|
|
18
|
-
return Promise<std::shared_ptr<ArrayBuffer>>::async(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
);
|
|
13
|
+
return Promise<std::shared_ptr<ArrayBuffer>>::async([this, nativePassword, nativeSalt, iterations, keylen, digest]() {
|
|
14
|
+
return this->pbkdf2Sync(nativePassword, nativeSalt, iterations, keylen, digest);
|
|
15
|
+
});
|
|
23
16
|
}
|
|
24
17
|
|
|
25
|
-
std::shared_ptr<ArrayBuffer>
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const std::string& digest
|
|
32
|
-
) {
|
|
33
|
-
size_t bufferSize = static_cast<size_t>(keylen);
|
|
34
|
-
uint8_t* data = new uint8_t[bufferSize];
|
|
35
|
-
auto result = std::make_shared<NativeArrayBuffer>(data, bufferSize, [=]() { delete[] data; });
|
|
18
|
+
std::shared_ptr<ArrayBuffer> HybridPbkdf2::pbkdf2Sync(const std::shared_ptr<ArrayBuffer>& password,
|
|
19
|
+
const std::shared_ptr<ArrayBuffer>& salt, double iterations, double keylen,
|
|
20
|
+
const std::string& digest) {
|
|
21
|
+
size_t bufferSize = static_cast<size_t>(keylen);
|
|
22
|
+
uint8_t* data = new uint8_t[bufferSize];
|
|
23
|
+
auto result = std::make_shared<NativeArrayBuffer>(data, bufferSize, [=]() { delete[] data; });
|
|
36
24
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
result.get()->data(), result.get()->size());
|
|
53
|
-
} else {
|
|
54
|
-
// fallback to OpenSSL
|
|
55
|
-
auto *digestByName = EVP_get_digestbyname(digest.c_str());
|
|
56
|
-
if (digestByName == nullptr) {
|
|
57
|
-
throw std::runtime_error("Invalid hash-algorithm: " + digest);
|
|
58
|
-
}
|
|
59
|
-
char *passAsCharA = reinterpret_cast<char *>(password.get()->data());
|
|
60
|
-
const unsigned char *saltAsCharA =
|
|
61
|
-
reinterpret_cast<const unsigned char *>(salt.get()->data());
|
|
62
|
-
unsigned char *resultAsCharA =
|
|
63
|
-
reinterpret_cast<unsigned char *>(result.get()->data());
|
|
64
|
-
PKCS5_PBKDF2_HMAC(passAsCharA, password.get()->size(), saltAsCharA,
|
|
65
|
-
salt.get()->size(), static_cast<uint32_t>(iterations),
|
|
66
|
-
digestByName, result.get()->size(), resultAsCharA);
|
|
25
|
+
// use fastpbkdf2 when possible
|
|
26
|
+
if (digest == "sha1") {
|
|
27
|
+
fastpbkdf2_hmac_sha1(password.get()->data(), password.get()->size(), salt.get()->data(), salt.get()->size(),
|
|
28
|
+
static_cast<uint32_t>(iterations), result.get()->data(), result.get()->size());
|
|
29
|
+
} else if (digest == "sha256") {
|
|
30
|
+
fastpbkdf2_hmac_sha256(password.get()->data(), password.get()->size(), salt.get()->data(), salt.get()->size(),
|
|
31
|
+
static_cast<uint32_t>(iterations), result.get()->data(), result.get()->size());
|
|
32
|
+
} else if (digest == "sha512") {
|
|
33
|
+
fastpbkdf2_hmac_sha512(password.get()->data(), password.get()->size(), salt.get()->data(), salt.get()->size(),
|
|
34
|
+
static_cast<uint32_t>(iterations), result.get()->data(), result.get()->size());
|
|
35
|
+
} else {
|
|
36
|
+
// fallback to OpenSSL
|
|
37
|
+
auto* digestByName = EVP_get_digestbyname(digest.c_str());
|
|
38
|
+
if (digestByName == nullptr) {
|
|
39
|
+
throw std::runtime_error("Invalid hash-algorithm: " + digest);
|
|
67
40
|
}
|
|
41
|
+
char* passAsCharA = reinterpret_cast<char*>(password.get()->data());
|
|
42
|
+
const unsigned char* saltAsCharA = reinterpret_cast<const unsigned char*>(salt.get()->data());
|
|
43
|
+
unsigned char* resultAsCharA = reinterpret_cast<unsigned char*>(result.get()->data());
|
|
44
|
+
PKCS5_PBKDF2_HMAC(passAsCharA, password.get()->size(), saltAsCharA, salt.get()->size(), static_cast<uint32_t>(iterations), digestByName,
|
|
45
|
+
result.get()->size(), resultAsCharA);
|
|
46
|
+
}
|
|
68
47
|
|
|
69
|
-
|
|
48
|
+
return result;
|
|
70
49
|
}
|
|
71
50
|
|
|
72
51
|
} // namespace margelo::nitro::crypto
|
|
@@ -13,23 +13,12 @@ class HybridPbkdf2 : public HybridPbkdf2Spec {
|
|
|
13
13
|
|
|
14
14
|
public:
|
|
15
15
|
// Methods
|
|
16
|
-
std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>>
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const std::shared_ptr<ArrayBuffer>& salt,
|
|
20
|
-
double iterations,
|
|
21
|
-
double keylen,
|
|
22
|
-
const std::string& digest
|
|
23
|
-
) override;
|
|
16
|
+
std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>> pbkdf2(const std::shared_ptr<ArrayBuffer>& password,
|
|
17
|
+
const std::shared_ptr<ArrayBuffer>& salt, double iterations, double keylen,
|
|
18
|
+
const std::string& digest) override;
|
|
24
19
|
|
|
25
|
-
std::shared_ptr<ArrayBuffer>
|
|
26
|
-
|
|
27
|
-
const std::shared_ptr<ArrayBuffer>& password,
|
|
28
|
-
const std::shared_ptr<ArrayBuffer>& salt,
|
|
29
|
-
double iterations,
|
|
30
|
-
double keylen,
|
|
31
|
-
const std::string& digest
|
|
32
|
-
) override;
|
|
20
|
+
std::shared_ptr<ArrayBuffer> pbkdf2Sync(const std::shared_ptr<ArrayBuffer>& password, const std::shared_ptr<ArrayBuffer>& salt,
|
|
21
|
+
double iterations, double keylen, const std::string& digest) override;
|
|
33
22
|
};
|
|
34
23
|
|
|
35
24
|
} // namespace margelo::nitro::crypto
|
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
#include "HybridRandom.hpp"
|
|
5
5
|
#include "Utils.hpp"
|
|
6
6
|
|
|
7
|
-
|
|
8
7
|
size_t checkSize(double size) {
|
|
9
8
|
if (!CheckIsUint32(size)) {
|
|
10
9
|
throw std::runtime_error("size must be uint32");
|
|
@@ -25,33 +24,23 @@ size_t checkOffset(double size, double offset) {
|
|
|
25
24
|
return static_cast<size_t>(offset);
|
|
26
25
|
}
|
|
27
26
|
|
|
28
|
-
|
|
29
27
|
namespace margelo::nitro::crypto {
|
|
30
28
|
|
|
31
|
-
std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>>
|
|
32
|
-
|
|
33
|
-
double dOffset,
|
|
34
|
-
double dSize) {
|
|
29
|
+
std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>> HybridRandom::randomFill(const std::shared_ptr<ArrayBuffer>& buffer, double dOffset,
|
|
30
|
+
double dSize) {
|
|
35
31
|
// get owned NativeArrayBuffer before passing to sync function
|
|
36
32
|
auto nativeBuffer = ToNativeArrayBuffer(buffer);
|
|
37
33
|
|
|
38
34
|
return Promise<std::shared_ptr<ArrayBuffer>>::async(
|
|
39
|
-
|
|
40
|
-
return this->randomFillSync(nativeBuffer, dOffset, dSize);
|
|
41
|
-
}
|
|
42
|
-
);
|
|
35
|
+
[this, nativeBuffer, dOffset, dSize]() { return this->randomFillSync(nativeBuffer, dOffset, dSize); });
|
|
43
36
|
};
|
|
44
37
|
|
|
45
|
-
std::shared_ptr<ArrayBuffer>
|
|
46
|
-
HybridRandom::randomFillSync(const std::shared_ptr<ArrayBuffer>& buffer,
|
|
47
|
-
double dOffset,
|
|
48
|
-
double dSize) {
|
|
38
|
+
std::shared_ptr<ArrayBuffer> HybridRandom::randomFillSync(const std::shared_ptr<ArrayBuffer>& buffer, double dOffset, double dSize) {
|
|
49
39
|
size_t size = checkSize(dSize);
|
|
50
40
|
size_t offset = checkOffset(dSize, dOffset);
|
|
51
41
|
uint8_t* data = buffer.get()->data();
|
|
52
42
|
if (RAND_bytes(data + offset, (int)size) != 1) {
|
|
53
|
-
throw std::runtime_error("error calling RAND_bytes" +
|
|
54
|
-
std::to_string(ERR_get_error()));
|
|
43
|
+
throw std::runtime_error("error calling RAND_bytes" + std::to_string(ERR_get_error()));
|
|
55
44
|
}
|
|
56
45
|
return buffer;
|
|
57
46
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#include <cmath>
|
|
2
2
|
#include <future>
|
|
3
|
-
#include <memory>
|
|
4
3
|
#include <iostream>
|
|
4
|
+
#include <memory>
|
|
5
5
|
|
|
6
6
|
#include "HybridRandomSpec.hpp"
|
|
7
7
|
|
|
@@ -15,17 +15,16 @@ class HybridRandom : public HybridRandomSpec {
|
|
|
15
15
|
|
|
16
16
|
public:
|
|
17
17
|
// Methods
|
|
18
|
-
std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>>
|
|
19
|
-
|
|
18
|
+
std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>> randomFill(const std::shared_ptr<ArrayBuffer>& buffer, double dOffset,
|
|
19
|
+
double dSize) override;
|
|
20
20
|
|
|
21
|
-
std::shared_ptr<ArrayBuffer>
|
|
22
|
-
randomFillSync(const std::shared_ptr<ArrayBuffer>& buffer, double dOffset, double dSize) override;
|
|
21
|
+
std::shared_ptr<ArrayBuffer> randomFillSync(const std::shared_ptr<ArrayBuffer>& buffer, double dOffset, double dSize) override;
|
|
23
22
|
};
|
|
24
23
|
|
|
25
24
|
inline void printData(std::string name, uint8_t* data, size_t size) {
|
|
26
25
|
std::cout << "data - " << name << std::endl;
|
|
27
26
|
for (size_t i = 0; i < size; i++) {
|
|
28
|
-
|
|
27
|
+
printf("%u ", data[i]);
|
|
29
28
|
}
|
|
30
29
|
printf("\n");
|
|
31
30
|
}
|
package/cpp/utils/Utils.hpp
CHANGED
|
@@ -3,8 +3,7 @@
|
|
|
3
3
|
#include <NitroModules/ArrayBuffer.hpp>
|
|
4
4
|
|
|
5
5
|
// copy a JSArrayBuffer that we do not own into a NativeArrayBuffer that we do own
|
|
6
|
-
inline std::shared_ptr<margelo::nitro::NativeArrayBuffer>
|
|
7
|
-
ToNativeArrayBuffer(const std::shared_ptr<margelo::nitro::ArrayBuffer>& buffer) {
|
|
6
|
+
inline std::shared_ptr<margelo::nitro::NativeArrayBuffer> ToNativeArrayBuffer(const std::shared_ptr<margelo::nitro::ArrayBuffer>& buffer) {
|
|
8
7
|
size_t bufferSize = buffer.get()->size();
|
|
9
8
|
uint8_t* data = new uint8_t[bufferSize];
|
|
10
9
|
memcpy(data, buffer.get()->data(), bufferSize);
|
package/lib/commonjs/hash.js
CHANGED
|
@@ -19,23 +19,26 @@ function getHashes() {
|
|
|
19
19
|
return HashUtils.getSupportedHashAlgorithms();
|
|
20
20
|
}
|
|
21
21
|
class Hash extends _readableStream.Stream.Transform {
|
|
22
|
+
validate(args) {
|
|
23
|
+
if (typeof args.algorithm !== 'string' || args.algorithm.length === 0) throw new Error('Algorithm must be a non-empty string');
|
|
24
|
+
if (args.options?.outputLength !== undefined && args.options.outputLength < 0) throw new Error('Output length must be a non-negative number');
|
|
25
|
+
if (args.options?.outputLength !== undefined && typeof args.options.outputLength !== 'number') throw new Error('Output length must be a number');
|
|
26
|
+
}
|
|
27
|
+
|
|
22
28
|
/**
|
|
23
29
|
* @internal use `createHash()` instead
|
|
24
30
|
*/
|
|
25
|
-
constructor({
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (native) {
|
|
34
|
-
this.native = native;
|
|
35
|
-
} else {
|
|
36
|
-
this.native = _reactNativeNitroModules.NitroModules.createHybridObject('Hash');
|
|
37
|
-
this.native.createHash(algorithm, this.options.outputLength);
|
|
31
|
+
constructor(args) {
|
|
32
|
+
super(args.options);
|
|
33
|
+
this.validate(args);
|
|
34
|
+
this.algorithm = args.algorithm;
|
|
35
|
+
this.options = args.options ?? {};
|
|
36
|
+
if (args.native) {
|
|
37
|
+
this.native = args.native;
|
|
38
|
+
return;
|
|
38
39
|
}
|
|
40
|
+
this.native = _reactNativeNitroModules.NitroModules.createHybridObject('Hash');
|
|
41
|
+
this.native.createHash(this.algorithm, this.options.outputLength);
|
|
39
42
|
}
|
|
40
43
|
|
|
41
44
|
/**
|
|
@@ -53,10 +56,7 @@ class Hash extends _readableStream.Stream.Transform {
|
|
|
53
56
|
const defaultEncoding = 'utf8';
|
|
54
57
|
inputEncoding = inputEncoding ?? defaultEncoding;
|
|
55
58
|
this.native.update((0, _utils.binaryLikeToArrayBuffer)(data, inputEncoding));
|
|
56
|
-
|
|
57
|
-
return this; // to support chaining syntax createHash().update().digest()
|
|
58
|
-
}
|
|
59
|
-
return Buffer.from([]); // returning empty buffer as _flush calls digest
|
|
59
|
+
return this; // to support chaining syntax createHash().update().digest()
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
/**
|
|
@@ -123,7 +123,7 @@ class Hash extends _readableStream.Stream.Transform {
|
|
|
123
123
|
|
|
124
124
|
// stream interface
|
|
125
125
|
_transform(chunk, encoding, callback) {
|
|
126
|
-
this.
|
|
126
|
+
this.update(chunk, encoding);
|
|
127
127
|
callback();
|
|
128
128
|
}
|
|
129
129
|
_flush(callback) {
|
|
@@ -131,6 +131,29 @@ class Hash extends _readableStream.Stream.Transform {
|
|
|
131
131
|
callback();
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Creates and returns a `Hash` object that can be used to generate hash digests
|
|
137
|
+
* using the given `algorithm`. Optional `options` argument controls stream
|
|
138
|
+
* behavior. For XOF hash functions such as `'shake256'`, the `outputLength` option
|
|
139
|
+
* can be used to specify the desired output length in bytes.
|
|
140
|
+
*
|
|
141
|
+
* The `algorithm` is dependent on the available algorithms supported by the
|
|
142
|
+
* version of OpenSSL on the platform. Examples are `'sha256'`, `'sha512'`, etc.
|
|
143
|
+
* On recent releases of OpenSSL, `openssl list -digest-algorithms` will
|
|
144
|
+
* display the available digest algorithms.
|
|
145
|
+
*
|
|
146
|
+
* Example: generating the sha256 sum of a file
|
|
147
|
+
*
|
|
148
|
+
* ```js
|
|
149
|
+
* import crypto from 'react-native-quick-crypto';
|
|
150
|
+
*
|
|
151
|
+
* const hash = crypto.createHash('sha256').update('Test123').digest('hex');
|
|
152
|
+
* console.log('SHA-256 of "Test123":', hash);
|
|
153
|
+
* ```
|
|
154
|
+
* @since v1.0.0
|
|
155
|
+
* @param options `stream.transform` options
|
|
156
|
+
*/
|
|
134
157
|
function createHash(algorithm, options) {
|
|
135
158
|
// @ts-expect-error private constructor
|
|
136
159
|
return new Hash({
|
package/lib/commonjs/hash.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_readableStream","require","_reactNativeNitroModules","_utils","HashUtils","native","NitroModules","createHybridObject","getSupportedHashAlgorithms","getHashes","Hash","Stream","Transform","
|
|
1
|
+
{"version":3,"names":["_readableStream","require","_reactNativeNitroModules","_utils","HashUtils","native","NitroModules","createHybridObject","getSupportedHashAlgorithms","getHashes","Hash","Stream","Transform","validate","args","algorithm","length","Error","options","outputLength","undefined","constructor","createHash","update","data","inputEncoding","defaultEncoding","binaryLikeToArrayBuffer","digest","encoding","nativeDigest","ab2str","Buffer","from","copy","newOptions","newNativeHash","hash","_transform","chunk","callback","_flush","push","hashExports","exports"],"sourceRoot":"../../src","sources":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CAAC;IAC/D,MAAMkB,IAAI,GAAG,IAAI3B,IAAI,CAAC;MACpBK,SAAS,EAAE,IAAI,CAACA,SAAS;MACzBG,OAAO,EAAEiB,UAAU;MACnB9B,MAAM,EAAE+B;IACV,CAAC,CAAC;IACF,OAAOC,IAAI;EACb;;EAEA;EACAC,UAAUA,CACRC,KAAiB,EACjBV,QAAwB,EACxBW,QAAoB,EACpB;IACA,IAAI,CAACjB,MAAM,CAACgB,KAAK,EAAEV,QAAoB,CAAC;IACxCW,QAAQ,CAAC,CAAC;EACZ;EACAC,MAAMA,CAACD,QAAoB,EAAE;IAC3B,IAAI,CAACE,IAAI,CAAC,IAAI,CAACd,MAAM,CAAC,CAAC,CAAC;IACxBY,QAAQ,CAAC,CAAC;EACZ;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASlB,UAAUA,CAACP,SAAiB,EAAEG,OAAqB,EAAQ;EACzE;EACA,OAAO,IAAIR,IAAI,CAAC;IACdK,SAAS;IACTG;EACF,CAAC,CAAC;AACJ;AAEO,MAAMyB,WAAW,GAAAC,OAAA,CAAAD,WAAA,GAAG;EACzBrB,UAAU;EACVb;AACF,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createHmac = createHmac;
|
|
7
|
+
exports.hmacExports = void 0;
|
|
8
|
+
var _reactNativeBuffer = require("@craftzdog/react-native-buffer");
|
|
9
|
+
var _readableStream = require("readable-stream");
|
|
10
|
+
var _reactNativeNitroModules = require("react-native-nitro-modules");
|
|
11
|
+
var _conversion = require("./utils/conversion");
|
|
12
|
+
class Hmac extends _readableStream.Stream.Transform {
|
|
13
|
+
validate(args) {
|
|
14
|
+
if (typeof args.algorithm !== 'string' || args.algorithm.length === 0) throw new Error('Algorithm must be a non-empty string');
|
|
15
|
+
if (args.key === null || args.key === undefined) throw new Error('Key must not be null or undefined');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @internal use `createHmac()` instead
|
|
20
|
+
*/
|
|
21
|
+
constructor(args) {
|
|
22
|
+
super(args.options);
|
|
23
|
+
this.validate(args);
|
|
24
|
+
this.algorithm = args.algorithm;
|
|
25
|
+
this.key = args.key;
|
|
26
|
+
this.native = _reactNativeNitroModules.NitroModules.createHybridObject('Hmac');
|
|
27
|
+
this.native.createHmac(this.algorithm, (0, _conversion.binaryLikeToArrayBuffer)(this.key));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Updates the `Hmac` content with the given `data`, the encoding of which is given in `inputEncoding`.
|
|
32
|
+
* If `encoding` is not provided, and the `data` is a string, an encoding of `'utf8'` is enforced.
|
|
33
|
+
* If `data` is a `Buffer`, `TypedArray`, or`DataView`, then `inputEncoding` is ignored.
|
|
34
|
+
*
|
|
35
|
+
* This can be called many times with new data as it is streamed.
|
|
36
|
+
* @since v1.0.0
|
|
37
|
+
* @param inputEncoding The `encoding` of the `data` string.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
update(data, inputEncoding) {
|
|
41
|
+
const defaultEncoding = 'utf8';
|
|
42
|
+
inputEncoding = inputEncoding ?? defaultEncoding;
|
|
43
|
+
this.native.update((0, _conversion.binaryLikeToArrayBuffer)(data, inputEncoding));
|
|
44
|
+
return this; // to support chaining syntax createHmac().update().digest()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Calculates the HMAC digest of all of the data passed using `hmac.update()`.
|
|
49
|
+
* If `encoding` is provided a string is returned; otherwise a `Buffer` is returned;
|
|
50
|
+
*
|
|
51
|
+
* The `Hmac` object can not be used again after `hmac.digest()` has been
|
|
52
|
+
* called. Multiple calls to `hmac.digest()` will result in an error being thrown.
|
|
53
|
+
* @since v1.0.0
|
|
54
|
+
* @param encoding The `encoding` of the return value.
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
digest(encoding) {
|
|
58
|
+
const nativeDigest = this.native.digest();
|
|
59
|
+
if (encoding && encoding !== 'buffer') {
|
|
60
|
+
return (0, _conversion.ab2str)(nativeDigest, encoding);
|
|
61
|
+
}
|
|
62
|
+
return _reactNativeBuffer.Buffer.from(nativeDigest);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// stream interface
|
|
66
|
+
_transform(chunk, encoding, callback) {
|
|
67
|
+
this.update(chunk, encoding);
|
|
68
|
+
callback();
|
|
69
|
+
}
|
|
70
|
+
_flush(callback) {
|
|
71
|
+
this.push(this.digest());
|
|
72
|
+
callback();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Creates and returns an `Hmac` object that uses the given `algorithm` and `key`.
|
|
78
|
+
* Optional `options` argument controls stream behavior.
|
|
79
|
+
*
|
|
80
|
+
* The `algorithm` is dependent on the available algorithms supported by the
|
|
81
|
+
* version of OpenSSL on the platform. Examples are `'sha256'`, `'sha512'`, etc.
|
|
82
|
+
* On recent releases of OpenSSL, `openssl list -digest-algorithms` will
|
|
83
|
+
* display the available digest algorithms.
|
|
84
|
+
*
|
|
85
|
+
* Example: generating the sha256 HMAC of a file
|
|
86
|
+
*
|
|
87
|
+
* ```js
|
|
88
|
+
* import crypto from 'react-native-quick-crypto';
|
|
89
|
+
*
|
|
90
|
+
* const hmac = crypto.createHmac('sha256', 'secret-key');
|
|
91
|
+
* hmac.update('message to hash');
|
|
92
|
+
* const digest = hmac.digest('hex');
|
|
93
|
+
* console.log(digest); // prints HMAC digest in hexadecimal format
|
|
94
|
+
* ```
|
|
95
|
+
* @since v1.0.0
|
|
96
|
+
* @param options `stream.transform` options
|
|
97
|
+
*/
|
|
98
|
+
function createHmac(algorithm, key, options) {
|
|
99
|
+
// @ts-expect-error private constructor
|
|
100
|
+
return new Hmac({
|
|
101
|
+
algorithm,
|
|
102
|
+
key,
|
|
103
|
+
options
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
const hmacExports = exports.hmacExports = {
|
|
107
|
+
createHmac
|
|
108
|
+
};
|
|
109
|
+
//# sourceMappingURL=hmac.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_reactNativeBuffer","require","_readableStream","_reactNativeNitroModules","_conversion","Hmac","Stream","Transform","validate","args","algorithm","length","Error","key","undefined","constructor","options","native","NitroModules","createHybridObject","createHmac","binaryLikeToArrayBuffer","update","data","inputEncoding","defaultEncoding","digest","encoding","nativeDigest","ab2str","Buffer","from","_transform","chunk","callback","_flush","push","hmacExports","exports"],"sourceRoot":"../../src","sources":["hmac.ts"],"mappings":";;;;;;;AAAA,IAAAA,kBAAA,GAAAC,OAAA;AACA,IAAAC,eAAA,GAAAD,OAAA;AACA,IAAAE,wBAAA,GAAAF,OAAA;AAIA,IAAAG,WAAA,GAAAH,OAAA;AAQA,MAAMI,IAAI,SAASC,sBAAM,CAACC,SAAS,CAAC;EAK1BC,QAAQA,CAACC,IAAc,EAAE;IAC/B,IAAI,OAAOA,IAAI,CAACC,SAAS,KAAK,QAAQ,IAAID,IAAI,CAACC,SAAS,CAACC,MAAM,KAAK,CAAC,EACnE,MAAM,IAAIC,KAAK,CAAC,sCAAsC,CAAC;IACzD,IAAIH,IAAI,CAACI,GAAG,KAAK,IAAI,IAAIJ,IAAI,CAACI,GAAG,KAAKC,SAAS,EAC7C,MAAM,IAAIF,KAAK,CAAC,mCAAmC,CAAC;EACxD;;EAEA;AACF;AACA;EACUG,WAAWA,CAACN,IAAc,EAAE;IAClC,KAAK,CAACA,IAAI,CAACO,OAAO,CAAC;IAEnB,IAAI,CAACR,QAAQ,CAACC,IAAI,CAAC;IAEnB,IAAI,CAACC,SAAS,GAAGD,IAAI,CAACC,SAAS;IAC/B,IAAI,CAACG,GAAG,GA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AACA;AACA;AACA;AACA;AACO,SAASd,UAAUA,CACxBV,SAAiB,EACjBG,GAAe,EACfG,OAA0B,EACpB;EACN;EACA,OAAO,IAAIX,IAAI,CAAC;IACdK,SAAS;IACTG,GAAG;IACHG;EACF,CAAC,CAAC;AACJ;AAEO,MAAMqB,WAAW,GAAAC,OAAA,CAAAD,WAAA,GAAG;EACzBjB;AACF,CAAC","ignoreList":[]}
|
package/lib/commonjs/index.js
CHANGED
|
@@ -21,6 +21,18 @@ Object.keys(_hash).forEach(function (key) {
|
|
|
21
21
|
}
|
|
22
22
|
});
|
|
23
23
|
});
|
|
24
|
+
var _hmac = require("./hmac");
|
|
25
|
+
Object.keys(_hmac).forEach(function (key) {
|
|
26
|
+
if (key === "default" || key === "__esModule") return;
|
|
27
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
28
|
+
if (key in exports && exports[key] === _hmac[key]) return;
|
|
29
|
+
Object.defineProperty(exports, key, {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
get: function () {
|
|
32
|
+
return _hmac[key];
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
});
|
|
24
36
|
var ed = _interopRequireWildcard(require("./ed"));
|
|
25
37
|
Object.keys(ed).forEach(function (key) {
|
|
26
38
|
if (key === "default" || key === "__esModule") return;
|
|
@@ -101,6 +113,7 @@ const QuickCrypto = {
|
|
|
101
113
|
// constants,
|
|
102
114
|
...keys,
|
|
103
115
|
..._hash.hashExports,
|
|
116
|
+
..._hmac.hmacExports,
|
|
104
117
|
...ed,
|
|
105
118
|
...pbkdf2,
|
|
106
119
|
...random,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_reactNativeBuffer","require","keys","_interopRequireWildcard","_hash","Object","forEach","key","prototype","hasOwnProperty","call","_exportNames","exports","defineProperty","enumerable","get","ed","pbkdf2","random","utils","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","n","__proto__","a","getOwnPropertyDescriptor","u","i","set","QuickCrypto","hash","install","global","Buffer","crypto","process","nextTick","setImmediate","_default","module"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;;;;;;;;AACA,IAAAA,kBAAA,GAAAC,OAAA;AAGA,IAAAC,IAAA,GAAAC,uBAAA,CAAAF,OAAA;AACA,IAAAG,KAAA,GAAAH,OAAA;
|
|
1
|
+
{"version":3,"names":["_reactNativeBuffer","require","keys","_interopRequireWildcard","_hash","Object","forEach","key","prototype","hasOwnProperty","call","_exportNames","exports","defineProperty","enumerable","get","_hmac","ed","pbkdf2","random","utils","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","n","__proto__","a","getOwnPropertyDescriptor","u","i","set","QuickCrypto","hash","hmac","install","global","Buffer","crypto","process","nextTick","setImmediate","_default","module"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;;;;;;;;AACA,IAAAA,kBAAA,GAAAC,OAAA;AAGA,IAAAC,IAAA,GAAAC,uBAAA,CAAAF,OAAA;AACA,IAAAG,KAAA,GAAAH,OAAA;AA4DAI,MAAA,CAAAH,IAAA,CAAAE,KAAA,EAAAE,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAF,MAAA,CAAAG,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAC,YAAA,EAAAJ,GAAA;EAAA,IAAAA,GAAA,IAAAK,OAAA,IAAAA,OAAA,CAAAL,GAAA,MAAAH,KAAA,CAAAG,GAAA;EAAAF,MAAA,CAAAQ,cAAA,CAAAD,OAAA,EAAAL,GAAA;IAAAO,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAX,KAAA,CAAAG,GAAA;IAAA;EAAA;AAAA;AA3DA,IAAAS,KAAA,GAAAf,OAAA;AA4DAI,MAAA,CAAAH,IAAA,CAAAc,KAAA,EAAAV,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAF,MAAA,CAAAG,SAAA,CAAAC,cAAA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;AAED;AAAAxB,OAAA,CAAA2B,OAAA,GAAAA,OAAA;AACAC,MAAM,CAACG,OAAO,CAACC,QAAQ,GAAGC,YAAY;;AAEtC;AAAA,IAAAC,QAAA,GAAAlC,OAAA,CAAAe,OAAA,GACeS,WAAW;AAQ1B;AACAW,MAAM,CAACnC,OAAO,GAAGwB,WAAW;AAC5BW,MAAM,CAACnC,OAAO,CAACe,OAAO,GAAGS,WAAW;AACpCW,MAAM,CAACnC,OAAO,CAAC2B,OAAO,GAAGA,OAAO","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../../src","sources":["specs/hmac.nitro.ts"],"mappings":"","ignoreList":[]}
|
package/lib/module/hash.js
CHANGED
|
@@ -13,23 +13,26 @@ export function getHashes() {
|
|
|
13
13
|
return HashUtils.getSupportedHashAlgorithms();
|
|
14
14
|
}
|
|
15
15
|
class Hash extends Stream.Transform {
|
|
16
|
+
validate(args) {
|
|
17
|
+
if (typeof args.algorithm !== 'string' || args.algorithm.length === 0) throw new Error('Algorithm must be a non-empty string');
|
|
18
|
+
if (args.options?.outputLength !== undefined && args.options.outputLength < 0) throw new Error('Output length must be a non-negative number');
|
|
19
|
+
if (args.options?.outputLength !== undefined && typeof args.options.outputLength !== 'number') throw new Error('Output length must be a number');
|
|
20
|
+
}
|
|
21
|
+
|
|
16
22
|
/**
|
|
17
23
|
* @internal use `createHash()` instead
|
|
18
24
|
*/
|
|
19
|
-
constructor({
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (native) {
|
|
28
|
-
this.native = native;
|
|
29
|
-
} else {
|
|
30
|
-
this.native = NitroModules.createHybridObject('Hash');
|
|
31
|
-
this.native.createHash(algorithm, this.options.outputLength);
|
|
25
|
+
constructor(args) {
|
|
26
|
+
super(args.options);
|
|
27
|
+
this.validate(args);
|
|
28
|
+
this.algorithm = args.algorithm;
|
|
29
|
+
this.options = args.options ?? {};
|
|
30
|
+
if (args.native) {
|
|
31
|
+
this.native = args.native;
|
|
32
|
+
return;
|
|
32
33
|
}
|
|
34
|
+
this.native = NitroModules.createHybridObject('Hash');
|
|
35
|
+
this.native.createHash(this.algorithm, this.options.outputLength);
|
|
33
36
|
}
|
|
34
37
|
|
|
35
38
|
/**
|
|
@@ -47,10 +50,7 @@ class Hash extends Stream.Transform {
|
|
|
47
50
|
const defaultEncoding = 'utf8';
|
|
48
51
|
inputEncoding = inputEncoding ?? defaultEncoding;
|
|
49
52
|
this.native.update(binaryLikeToArrayBuffer(data, inputEncoding));
|
|
50
|
-
|
|
51
|
-
return this; // to support chaining syntax createHash().update().digest()
|
|
52
|
-
}
|
|
53
|
-
return Buffer.from([]); // returning empty buffer as _flush calls digest
|
|
53
|
+
return this; // to support chaining syntax createHash().update().digest()
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
/**
|
|
@@ -117,7 +117,7 @@ class Hash extends Stream.Transform {
|
|
|
117
117
|
|
|
118
118
|
// stream interface
|
|
119
119
|
_transform(chunk, encoding, callback) {
|
|
120
|
-
this.
|
|
120
|
+
this.update(chunk, encoding);
|
|
121
121
|
callback();
|
|
122
122
|
}
|
|
123
123
|
_flush(callback) {
|
|
@@ -125,6 +125,29 @@ class Hash extends Stream.Transform {
|
|
|
125
125
|
callback();
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Creates and returns a `Hash` object that can be used to generate hash digests
|
|
131
|
+
* using the given `algorithm`. Optional `options` argument controls stream
|
|
132
|
+
* behavior. For XOF hash functions such as `'shake256'`, the `outputLength` option
|
|
133
|
+
* can be used to specify the desired output length in bytes.
|
|
134
|
+
*
|
|
135
|
+
* The `algorithm` is dependent on the available algorithms supported by the
|
|
136
|
+
* version of OpenSSL on the platform. Examples are `'sha256'`, `'sha512'`, etc.
|
|
137
|
+
* On recent releases of OpenSSL, `openssl list -digest-algorithms` will
|
|
138
|
+
* display the available digest algorithms.
|
|
139
|
+
*
|
|
140
|
+
* Example: generating the sha256 sum of a file
|
|
141
|
+
*
|
|
142
|
+
* ```js
|
|
143
|
+
* import crypto from 'react-native-quick-crypto';
|
|
144
|
+
*
|
|
145
|
+
* const hash = crypto.createHash('sha256').update('Test123').digest('hex');
|
|
146
|
+
* console.log('SHA-256 of "Test123":', hash);
|
|
147
|
+
* ```
|
|
148
|
+
* @since v1.0.0
|
|
149
|
+
* @param options `stream.transform` options
|
|
150
|
+
*/
|
|
128
151
|
export function createHash(algorithm, options) {
|
|
129
152
|
// @ts-expect-error private constructor
|
|
130
153
|
return new Hash({
|