react-native-quick-crypto 0.3.0 → 0.4.0

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.
Files changed (88) hide show
  1. package/README.md +25 -14
  2. package/android/CMakeLists.txt +10 -2
  3. package/android/gradle.properties +1 -1
  4. package/android/src/main/AndroidManifest.xml +1 -1
  5. package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoModule.java +70 -0
  6. package/android/src/main/java/com/{reactnativequickcrypto → margelo/quickcrypto}/QuickCryptoPackage.java +11 -12
  7. package/cpp/Cipher/MGLCipherHostObject.cpp +4 -5
  8. package/cpp/Cipher/MGLCreateCipherInstaller.cpp +1 -3
  9. package/cpp/Cipher/MGLGenerateKeyPairInstaller.h +6 -3
  10. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.h +5 -3
  11. package/cpp/Cipher/MGLPublicCipher.h +1 -1
  12. package/cpp/Cipher/MGLPublicCipherInstaller.h +1 -1
  13. package/cpp/Cipher/MGLRsa.h +5 -1
  14. package/cpp/JSIUtils/MGLJSIMacros.h +69 -6
  15. package/cpp/{Cipher/MGLCipherKeys.cpp → MGLKeys.cpp} +47 -49
  16. package/cpp/{Cipher/MGLCipherKeys.h → MGLKeys.h} +29 -30
  17. package/cpp/MGLQuickCryptoHostObject.cpp +12 -0
  18. package/cpp/Sig/MGLSignHostObjects.cpp +889 -0
  19. package/cpp/Sig/MGLSignHostObjects.h +88 -0
  20. package/cpp/Sig/MGLSignInstaller.cpp +24 -0
  21. package/cpp/Sig/MGLSignInstaller.h +29 -0
  22. package/cpp/Sig/MGLVerifyInstaller.cpp +24 -0
  23. package/cpp/Sig/MGLVerifyInstaller.h +22 -0
  24. package/cpp/Utils/MGLUtils.cpp +67 -29
  25. package/cpp/Utils/MGLUtils.h +17 -17
  26. package/lib/commonjs/@types/crypto-browserify.d.js +2 -0
  27. package/lib/commonjs/@types/crypto-browserify.d.js.map +1 -0
  28. package/lib/commonjs/@types/stream-browserify.d.js +2 -0
  29. package/lib/commonjs/@types/stream-browserify.d.js.map +1 -0
  30. package/lib/commonjs/Cipher.js +3 -2
  31. package/lib/commonjs/Cipher.js.map +1 -1
  32. package/lib/commonjs/Hash.js +2 -2
  33. package/lib/commonjs/Hash.js.map +1 -1
  34. package/lib/commonjs/Hmac.js +2 -2
  35. package/lib/commonjs/Hmac.js.map +1 -1
  36. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  37. package/lib/commonjs/NativeQuickCrypto/sig.js +2 -0
  38. package/lib/commonjs/NativeQuickCrypto/sig.js.map +1 -0
  39. package/lib/commonjs/QuickCrypto.js +4 -0
  40. package/lib/commonjs/QuickCrypto.js.map +1 -1
  41. package/lib/commonjs/index.js +16 -13
  42. package/lib/commonjs/index.js.map +1 -1
  43. package/lib/commonjs/keys.js +1 -4
  44. package/lib/commonjs/keys.js.map +1 -1
  45. package/lib/commonjs/sig.js +170 -0
  46. package/lib/commonjs/sig.js.map +1 -0
  47. package/lib/module/@types/crypto-browserify.d.js +2 -0
  48. package/lib/module/@types/crypto-browserify.d.js.map +1 -0
  49. package/lib/module/@types/stream-browserify.d.js +2 -0
  50. package/lib/module/@types/stream-browserify.d.js.map +1 -0
  51. package/lib/module/Cipher.js +2 -1
  52. package/lib/module/Cipher.js.map +1 -1
  53. package/lib/module/Hash.js +1 -1
  54. package/lib/module/Hash.js.map +1 -1
  55. package/lib/module/Hmac.js +1 -1
  56. package/lib/module/Hmac.js.map +1 -1
  57. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  58. package/lib/module/NativeQuickCrypto/sig.js +2 -0
  59. package/lib/module/NativeQuickCrypto/sig.js.map +1 -0
  60. package/lib/module/QuickCrypto.js +3 -0
  61. package/lib/module/QuickCrypto.js.map +1 -1
  62. package/lib/module/index.js +12 -2
  63. package/lib/module/index.js.map +1 -1
  64. package/lib/module/keys.js +1 -4
  65. package/lib/module/keys.js.map +1 -1
  66. package/lib/module/sig.js +155 -0
  67. package/lib/module/sig.js.map +1 -0
  68. package/lib/typescript/Cipher.d.ts +1 -1
  69. package/lib/typescript/Hash.d.ts +1 -1
  70. package/lib/typescript/Hmac.d.ts +1 -1
  71. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +3 -0
  72. package/lib/typescript/NativeQuickCrypto/sig.d.ts +12 -0
  73. package/lib/typescript/QuickCrypto.d.ts +3 -0
  74. package/lib/typescript/index.d.ts +207 -3
  75. package/lib/typescript/sig.d.ts +35 -0
  76. package/package.json +5 -3
  77. package/src/@types/crypto-browserify.d.ts +4 -0
  78. package/src/@types/stream-browserify.d.ts +4 -0
  79. package/src/Cipher.ts +2 -1
  80. package/src/Hash.ts +1 -1
  81. package/src/Hmac.ts +1 -1
  82. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +3 -0
  83. package/src/NativeQuickCrypto/sig.ts +17 -0
  84. package/src/QuickCrypto.ts +3 -0
  85. package/src/index.ts +12 -2
  86. package/src/keys.ts +18 -13
  87. package/src/sig.ts +179 -0
  88. package/android/src/main/java/com/reactnativequickcrypto/QuickCryptoModule.java +0 -70
package/src/sig.ts ADDED
@@ -0,0 +1,179 @@
1
+ import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';
2
+ import type { InternalSign, InternalVerify } from './NativeQuickCrypto/sig';
3
+ import Stream from 'stream';
4
+
5
+ // TODO(osp) same as publicCipher on node this are defined on C++ and exposed to node
6
+ // Do the same here
7
+ enum DSASigEnc {
8
+ kSigEncDER,
9
+ kSigEncP1363,
10
+ }
11
+
12
+ import {
13
+ BinaryLike,
14
+ binaryLikeToArrayBuffer,
15
+ getDefaultEncoding,
16
+ } from './Utils';
17
+ import { preparePrivateKey, preparePublicOrPrivateKey } from './keys';
18
+
19
+ const createInternalSign = NativeQuickCrypto.createSign;
20
+ const createInternalVerify = NativeQuickCrypto.createVerify;
21
+
22
+ function getPadding(options: any) {
23
+ return getIntOption('padding', options);
24
+ }
25
+
26
+ function getSaltLength(options: any) {
27
+ return getIntOption('saltLength', options);
28
+ }
29
+
30
+ function getDSASignatureEncoding(options: any) {
31
+ if (typeof options === 'object') {
32
+ const { dsaEncoding = 'der' } = options;
33
+ if (dsaEncoding === 'der') return DSASigEnc.kSigEncDER;
34
+ else if (dsaEncoding === 'ieee-p1363') return DSASigEnc.kSigEncP1363;
35
+ throw new Error(`options.dsaEncoding: ${dsaEncoding} not a valid encoding`);
36
+ }
37
+
38
+ return DSASigEnc.kSigEncDER;
39
+ }
40
+
41
+ function getIntOption(name: string, options: any) {
42
+ const value = options[name];
43
+ if (value !== undefined) {
44
+ if (value === value >> 0) {
45
+ return value;
46
+ }
47
+ throw new Error(`options.${name}: ${value} not a valid int value`);
48
+ }
49
+ return undefined;
50
+ }
51
+
52
+ class Verify extends Stream.Writable {
53
+ private internal: InternalVerify;
54
+ constructor(algorithm: string, options: Stream.WritableOptions) {
55
+ super(options);
56
+ this.internal = createInternalVerify();
57
+ this.internal.init(algorithm);
58
+ }
59
+
60
+ _write(chunk: BinaryLike, encoding: string, callback: () => void) {
61
+ this.update(chunk, encoding);
62
+ callback();
63
+ }
64
+
65
+ update(data: BinaryLike, encoding?: string) {
66
+ encoding = encoding ?? getDefaultEncoding();
67
+ data = binaryLikeToArrayBuffer(data, encoding);
68
+ this.internal.update(data);
69
+ return this;
70
+ }
71
+
72
+ verify(
73
+ options: {
74
+ key: string | Buffer;
75
+ format?: string;
76
+ type?: string;
77
+ passphrase?: string;
78
+ padding?: number;
79
+ saltLength?: number;
80
+ },
81
+ signature: BinaryLike
82
+ ): boolean {
83
+ if (!options) {
84
+ throw new Error('Crypto sign key required');
85
+ }
86
+
87
+ const { data, format, type, passphrase } =
88
+ preparePublicOrPrivateKey(options);
89
+
90
+ const rsaPadding = getPadding(options);
91
+ const pssSaltLength = getSaltLength(options);
92
+
93
+ // Options specific to (EC)DSA
94
+ const dsaSigEnc = getDSASignatureEncoding(options);
95
+
96
+ const ret = this.internal.verify(
97
+ data,
98
+ format,
99
+ type,
100
+ passphrase,
101
+ binaryLikeToArrayBuffer(signature),
102
+ rsaPadding,
103
+ pssSaltLength,
104
+ dsaSigEnc
105
+ );
106
+
107
+ return ret;
108
+ }
109
+ }
110
+
111
+ class Sign extends Stream.Writable {
112
+ private internal: InternalSign;
113
+ constructor(algorithm: string, options: Stream.WritableOptions) {
114
+ super(options);
115
+ this.internal = createInternalSign();
116
+ this.internal.init(algorithm);
117
+ }
118
+
119
+ _write(chunk: BinaryLike, encoding: string, callback: () => void) {
120
+ this.update(chunk, encoding);
121
+ callback();
122
+ }
123
+
124
+ update(data: BinaryLike, encoding?: string) {
125
+ encoding = encoding ?? getDefaultEncoding();
126
+ data = binaryLikeToArrayBuffer(data, encoding);
127
+ this.internal.update(data);
128
+ return this;
129
+ }
130
+
131
+ sign(
132
+ options: {
133
+ key: string | Buffer;
134
+ format?: string;
135
+ type?: string;
136
+ passphrase?: string;
137
+ padding?: number;
138
+ saltLength?: number;
139
+ },
140
+ encoding?: string
141
+ ) {
142
+ if (!options) {
143
+ throw new Error('Crypto sign key required');
144
+ }
145
+
146
+ const { data, format, type, passphrase } = preparePrivateKey(options);
147
+
148
+ const rsaPadding = getPadding(options);
149
+ const pssSaltLength = getSaltLength(options);
150
+
151
+ // Options specific to (EC)DSA
152
+ const dsaSigEnc = getDSASignatureEncoding(options);
153
+
154
+ const ret = this.internal.sign(
155
+ data,
156
+ format,
157
+ type,
158
+ passphrase,
159
+ rsaPadding,
160
+ pssSaltLength,
161
+ dsaSigEnc
162
+ );
163
+
164
+ encoding = encoding || getDefaultEncoding();
165
+ if (encoding && encoding !== 'buffer') {
166
+ return Buffer.from(ret).toString(encoding as any);
167
+ }
168
+
169
+ return Buffer.from(ret);
170
+ }
171
+ }
172
+
173
+ export function createSign(algorithm: string, options?: any) {
174
+ return new Sign(algorithm, options);
175
+ }
176
+
177
+ export function createVerify(algorithm: string, options?: any) {
178
+ return new Verify(algorithm, options);
179
+ }
@@ -1,70 +0,0 @@
1
- package com.reactnativequickcrypto;
2
-
3
- import android.util.Log;
4
-
5
- import androidx.annotation.NonNull;
6
-
7
- import com.facebook.jni.HybridData;
8
- import com.facebook.proguard.annotations.DoNotStrip;
9
- import com.facebook.react.bridge.JavaScriptContextHolder;
10
- import com.facebook.react.bridge.ReactContextBaseJavaModule;
11
- import com.facebook.react.bridge.ReactApplicationContext;
12
- import com.facebook.react.bridge.ReactMethod;
13
- import com.facebook.react.module.annotations.ReactModule;
14
- import com.facebook.react.turbomodule.core.CallInvokerHolderImpl;
15
-
16
- @ReactModule(name = QuickCryptoModule.NAME)
17
- public class QuickCryptoModule extends ReactContextBaseJavaModule {
18
- public static final String NAME = "QuickCrypto";
19
-
20
- @DoNotStrip
21
- private HybridData mHybridData;
22
-
23
- private native HybridData initHybrid();
24
-
25
- public QuickCryptoModule(ReactApplicationContext reactContext) {
26
- super(reactContext);
27
- }
28
-
29
- @NonNull
30
- @Override
31
- public String getName() {
32
- return NAME;
33
- }
34
-
35
- @ReactMethod(isBlockingSynchronousMethod = true)
36
- public boolean install() {
37
- try {
38
- if (mHybridData != null) {
39
- return false;
40
- }
41
- Log.i(NAME, "Loading C++ library...");
42
- System.loadLibrary("reactnativequickcrypto");
43
-
44
- JavaScriptContextHolder jsContext = getReactApplicationContext().getJavaScriptContextHolder();
45
- CallInvokerHolderImpl jsCallInvokerHolder = (CallInvokerHolderImpl) getReactApplicationContext()
46
- .getCatalystInstance()
47
- .getJSCallInvokerHolder();
48
-
49
-
50
- Log.i(NAME, "Installing JSI Bindings for react-native-quick-crypto...");
51
- mHybridData = initHybrid();
52
- nativeInstall(jsContext.get(), jsCallInvokerHolder);
53
- Log.i(NAME, "Successfully installed JSI Bindings for react-native-quick-crypto!");
54
-
55
- return true;
56
- } catch (Exception exception) {
57
- Log.e(NAME, "Failed to install JSI Bindings for react-native-quick-crypto!", exception);
58
- return false;
59
- }
60
- }
61
-
62
- public void destroy() {
63
- if (mHybridData == null) {
64
- return;
65
- }
66
- mHybridData.resetNative();
67
- }
68
-
69
- private native void nativeInstall(long jsiPtr, CallInvokerHolderImpl jsCallInvokerHolder);
70
- }