react-native-quick-crypto 0.4.1 → 0.4.4
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/LICENSE +7 -1
- package/README.md +61 -5
- package/android/build.gradle +17 -2
- package/cpp/Cipher/MGLPublicCipher.h +1 -1
- package/cpp/MGLKeys.cpp +1 -1
- package/lib/commonjs/random.js.map +1 -1
- package/lib/commonjs/sig.js +3 -3
- package/lib/commonjs/sig.js.map +1 -1
- package/lib/module/random.js.map +1 -1
- package/lib/module/sig.js +1 -1
- package/lib/module/sig.js.map +1 -1
- package/lib/typescript/sig.d.ts +1 -1
- package/package.json +2 -3
- package/react-native-quick-crypto.podspec +1 -1
- package/src/random.ts +1 -1
- package/src/sig.ts +1 -1
package/LICENSE
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
**react-native-quick-crypto**
|
|
2
|
+
|
|
1
3
|
MIT License
|
|
2
4
|
|
|
3
|
-
Copyright (c) 2021
|
|
5
|
+
Copyright (c) 2021 Margelo GmbH
|
|
4
6
|
|
|
5
7
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
8
|
of this software and associated documentation files (the "Software"), to deal
|
|
@@ -19,3 +21,7 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
19
21
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
22
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
23
|
SOFTWARE.
|
|
24
|
+
|
|
25
|
+
**NodeJS Crypto**
|
|
26
|
+
|
|
27
|
+
See https://github.com/nodejs/node/blob/main/LICENSE
|
package/README.md
CHANGED
|
@@ -47,6 +47,7 @@ Creating a Wallet took 289 ms
|
|
|
47
47
|
|
|
48
48
|
```sh
|
|
49
49
|
yarn add react-native-quick-crypto
|
|
50
|
+
yarn add react-native-quick-base64
|
|
50
51
|
cd ios && pod install
|
|
51
52
|
```
|
|
52
53
|
|
|
@@ -56,16 +57,17 @@ cd ios && pod install
|
|
|
56
57
|
|
|
57
58
|
```sh
|
|
58
59
|
expo install react-native-quick-crypto
|
|
60
|
+
expo install react-native-quick-base64
|
|
59
61
|
expo prebuild
|
|
60
62
|
```
|
|
61
63
|
|
|
62
|
-
##
|
|
64
|
+
## Replace `crypto-browserify`
|
|
65
|
+
|
|
66
|
+
If you are using a library that depends on `crypto`, instead of polyfilling it with `crypto-browserify` (or `react-native-crypto`) you can use `react-native-quick-crypto` for a fully native implementation. This way you can get much faster crypto operations with just a single-line change!
|
|
63
67
|
|
|
64
68
|
In your `babel.config.js`, add a module resolver to replace `crypto` with `react-native-quick-crypto`:
|
|
65
69
|
|
|
66
70
|
```diff
|
|
67
|
-
+const path = require('path');
|
|
68
|
-
|
|
69
71
|
module.exports = {
|
|
70
72
|
presets: ['module:metro-react-native-babel-preset'],
|
|
71
73
|
plugins: [
|
|
@@ -74,6 +76,8 @@ module.exports = {
|
|
|
74
76
|
+ {
|
|
75
77
|
+ alias: {
|
|
76
78
|
+ 'crypto': 'react-native-quick-crypto',
|
|
79
|
+
+ 'stream': 'stream-browserify',
|
|
80
|
+
+ 'buffer': '@craftzdog/react-native-buffer',
|
|
77
81
|
+ },
|
|
78
82
|
+ },
|
|
79
83
|
+ ],
|
|
@@ -82,18 +86,69 @@ module.exports = {
|
|
|
82
86
|
};
|
|
83
87
|
```
|
|
84
88
|
|
|
89
|
+
Then restart your bundler using `yarn start --reset-cache`.
|
|
90
|
+
|
|
85
91
|
Now, all imports for `crypto` will be resolved as `react-native-quick-crypto` instead.
|
|
86
92
|
|
|
93
|
+
> 💡 Since react-native-quick-crypto depends on `stream` and `buffer`, we can resolve those to `stream-browserify` and @craftzdog's `react-native-buffer` (which is faster than `buffer` because it uses JSI for base64 encoding and decoding).
|
|
94
|
+
|
|
95
|
+
## Usage
|
|
96
|
+
|
|
97
|
+
For example, to hash a string with SHA256 you can do the following:
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import Crypto from 'react-native-quick-crypto'
|
|
101
|
+
|
|
102
|
+
const hashed = Crypto.createHash('sha256')
|
|
103
|
+
.update('Damn, Margelo writes hella good software!')
|
|
104
|
+
.digest('hex')
|
|
105
|
+
```
|
|
106
|
+
|
|
87
107
|
---
|
|
88
108
|
|
|
89
109
|
## Sponsors
|
|
90
110
|
|
|
111
|
+
<!-- Onin -->
|
|
91
112
|
<div align="center">
|
|
113
|
+
<img height="50" src="./img/sponsors/onin.svg" align="center"><br/>
|
|
114
|
+
<a href="https://onin.co"><b>Onin</b></a> - This library is supported by Onin. Plan events without leaving the chat: <a href="https://onin.co">onin.co</a>
|
|
115
|
+
</div>
|
|
116
|
+
<br/>
|
|
117
|
+
<br/>
|
|
118
|
+
|
|
119
|
+
<!-- Steakwallet -->
|
|
120
|
+
<div align="center">
|
|
121
|
+
<img height="37" src="./img/sponsors/omni.png" align="center"><br/>
|
|
122
|
+
<a href="https://steakwallet.fi"><b>Omni</b></a> - Web3 for all. Access all of Web3 in one easy to use wallet. Omni supports more blockchains so you get more tokens, more yields, more NFTs, and more fun!
|
|
123
|
+
</div>
|
|
124
|
+
<br/>
|
|
125
|
+
<br/>
|
|
92
126
|
|
|
93
|
-
|
|
127
|
+
<!-- Litentry -->
|
|
128
|
+
<div align="center">
|
|
129
|
+
<img height="70" src="./img/sponsors/litentry.png" align="center"><br/>
|
|
94
130
|
<a href="https://litentry.com"><b>Litentry</b></a> - A decentralized identity aggregator, providing the structure and tools to empower you and your identity.
|
|
131
|
+
</div>
|
|
132
|
+
<br/>
|
|
133
|
+
<br/>
|
|
95
134
|
|
|
135
|
+
<!-- WalletConnect -->
|
|
136
|
+
<div align="center">
|
|
137
|
+
<img height="35" src="./img/sponsors/walletconnect.png" align="center"><br/>
|
|
138
|
+
<a href="https://walletconnect.com"><b>WalletConnect</b></a> - The communications protocol for web3, WalletConnect brings the ecosystem together by enabling wallets and apps to securely connect and interact.
|
|
139
|
+
</div>
|
|
140
|
+
<br/>
|
|
141
|
+
<br/>
|
|
142
|
+
|
|
143
|
+
<!-- WalletConnect -->
|
|
144
|
+
|
|
145
|
+
<!-- THORSwap -->
|
|
146
|
+
<div align="center">
|
|
147
|
+
<img height="40" src="./img/sponsors/thorswap.png" align="center"><br/>
|
|
148
|
+
<a href="https://thorswap.finance"><b>THORSwap</b></a> - THORSwap is a cross-chain DEX aggregator that enables users to swap native assets across chains, provide liquidity to earn yield, and more. THORSwap is fully permissionless and non-custodial. No account signup, your wallet, your keys, your coins.
|
|
96
149
|
</div>
|
|
150
|
+
<br/>
|
|
151
|
+
<br/>
|
|
97
152
|
|
|
98
153
|
## Limitations
|
|
99
154
|
|
|
@@ -109,4 +164,5 @@ See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the
|
|
|
109
164
|
|
|
110
165
|
## License
|
|
111
166
|
|
|
112
|
-
MIT
|
|
167
|
+
- react-native-quick-crypto is licensed under MIT.
|
|
168
|
+
- react-native-quick-crypto is heavily inspired by NodeJS Crypto, which is licensed under [nodejs/LICENSE](https://github.com/nodejs/node/blob/main/LICENSE).
|
package/android/build.gradle
CHANGED
|
@@ -177,7 +177,17 @@ dependencies {
|
|
|
177
177
|
extractJNI("com.facebook.fbjni:fbjni:+")
|
|
178
178
|
|
|
179
179
|
if (!sourceBuild) {
|
|
180
|
-
def
|
|
180
|
+
def buildType = "debug"
|
|
181
|
+
tasks.all({ task ->
|
|
182
|
+
if (task.name == "buildCMakeRelease") {
|
|
183
|
+
buildType = "release"
|
|
184
|
+
}
|
|
185
|
+
})
|
|
186
|
+
def rnAarMatcher = "**/react-native/**/*${buildType}.aar"
|
|
187
|
+
if (REACT_NATIVE_VERSION < 69) {
|
|
188
|
+
rnAarMatcher = "**/**/*.aar"
|
|
189
|
+
}
|
|
190
|
+
def rnAAR = fileTree("${defaultDir.toString()}").matching({ it.include rnAarMatcher }).singleFile
|
|
181
191
|
extractJNI(files(rnAAR))
|
|
182
192
|
}
|
|
183
193
|
}
|
|
@@ -208,7 +218,12 @@ task createNativeDepsDirectories {
|
|
|
208
218
|
}
|
|
209
219
|
|
|
210
220
|
task downloadBoost(dependsOn: createNativeDepsDirectories, type: Download) {
|
|
211
|
-
|
|
221
|
+
def transformedVersion = BOOST_VERSION.replace("_", ".")
|
|
222
|
+
def srcUrl = "https://boostorg.jfrog.io/artifactory/main/release/${transformedVersion}/source/boost_${BOOST_VERSION}.tar.gz"
|
|
223
|
+
if (REACT_NATIVE_VERSION < 69) {
|
|
224
|
+
srcUrl = "https://github.com/react-native-community/boost-for-react-native/releases/download/v${transformedVersion}-0/boost_${BOOST_VERSION}.tar.gz"
|
|
225
|
+
}
|
|
226
|
+
src(srcUrl)
|
|
212
227
|
onlyIfNewer(true)
|
|
213
228
|
overwrite(false)
|
|
214
229
|
dest(boost_file)
|
package/cpp/MGLKeys.cpp
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["random.ts"],"names":["random","NativeQuickCrypto","randomFill","buffer","Error","callback","offset","size","byteLength","then","e","randomFillSync","randomBytes","buf","Buffer","undefined","error","rng","pseudoRandomBytes","prng","RAND_MAX","randomCache","randomCacheOffset","length","asyncCacheFillInProgress","asyncCachePendingTasks","randomInt","arg1","arg2","max","min","minNotSpecified","isSync","Number","isSafeInteger","range","randLimit","x","readUIntBE","n","process","nextTick","push","asyncRefillRandomIntCache","err","tasks","errorReceiver","shift","splice","forEach","task","getRandomValues","data"],"mappings":";;;;;;;;;;;;;AAAA;;AACA;;AACA;;AAEA,MAAMA,MAAM,GAAGC,qCAAkBD,MAAjC;;AAgCO,SAASE,UAAT,CAAoBC,MAApB,EAAuD;AAAA;;AAC5D,MAAI,eAAY,qDAAc,CAA1B,8EAAiC,UAArC,EAAiD;AAC/C,UAAM,IAAIC,KAAJ,CAAU,oCAAV,CAAN;AACD;;AAED,QAAMC,QAAQ,YAAQ,qDAAc,CAAtB,4EAAd;A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B,KAAK,GAAGpB,sBAAd;AACA,UAAMqB,aAAa,GAAGF,GAAG,IAAIC,KAAK,CAACE,KAAN,EAA7B;AACA,QAAI,CAACH,GAAL,EAAUtB,iBAAiB,GAAG,CAApB,CALqB,CAO/B;AACA;AACA;AACA;;AACAuB,IAAAA,KAAK,CAACG,MAAN,CAAa,CAAb,EAAgBC,OAAhB,CAAyBC,IAAD,IAAU;AAChCxB,MAAAA,SAAS,CAACwB,IAAI,CAACpB,GAAN,EAAWoB,IAAI,CAACrB,GAAhB,EAAqBqB,IAAI,CAAC7C,QAA1B,CAAT;AACD,KAFD,EAX+B,CAe/B;;AACA,QAAIyC,aAAJ,EAAmBA,aAAa,CAACzC,QAAd,CAAuBuC,GAAvB,EAA4B,CAA5B;AACpB,GAjBS,CAAV;AAkBD,C,CAED;AACA;AACA;AACA;AACA;;;AAQO,SAASO,eAAT,CAAyBC,IAAzB,EAAyC;AAC9C,MAAIA,IAAI,CAAC5C,UAAL,GAAkB,KAAtB,EAA6B;AAC3B,UAAM,IAAIJ,KAAJ,CAAU,2CAAV,CAAN;AACD;;AACDO,EAAAA,cAAc,CAACyC,IAAD,EAAO,CAAP,CAAd;AACA,SAAOA,IAAP;AACD","sourcesContent":["import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';\nimport { Buffer } from '@craftzdog/react-native-buffer';\nimport { isBuffer } from '../src/Utils';\n\nconst random = NativeQuickCrypto.random;\n\ntype TypedArray =\n | Uint8Array\n | Uint8ClampedArray\n | Uint16Array\n | Uint32Array\n | Int8Array\n | Int16Array\n | Int32Array\n | Float32Array\n | Float64Array;\ntype ArrayBufferView = TypedArray | DataView | ArrayBufferLike | Buffer;\n\nexport function randomFill<T extends ArrayBufferView>(\n buffer: T,\n callback: (err: Error | null, buf: T) => void\n): void;\n\nexport function randomFill<T extends ArrayBufferView>(\n buffer: T,\n offset: number,\n callback: (err: Error | null, buf: T) => void\n): void;\n\nexport function randomFill<T extends ArrayBufferView>(\n buffer: T,\n offset: number,\n size: number,\n callback: (err: Error | null, buf: T) => void\n): void;\n\nexport function randomFill(buffer: any, ...rest: any[]): void {\n if (typeof rest[rest.length - 1] !== 'function') {\n throw new Error('No callback provided to randomDill');\n }\n\n const callback = rest[rest.length - 1] as any as (\n err: Error | null,\n buf?: ArrayBuffer\n ) => void;\n\n let offset: number = 0;\n let size: number = buffer.byteLength;\n\n if (typeof rest[2] === 'function') {\n offset = rest[0];\n size = rest[1];\n }\n\n if (typeof rest[1] === 'function') {\n offset = rest[0];\n }\n\n random\n .randomFill(isBuffer(buffer) ? buffer.buffer : buffer, offset, size)\n .then(\n () => {\n callback(null, buffer);\n },\n (e: Error) => {\n callback(e);\n }\n );\n}\n\nexport function randomFillSync<T extends ArrayBufferView>(\n buffer: T,\n offset?: number,\n size?: number\n): T;\n\nexport function randomFillSync(buffer: any, offset: number = 0, size?: number) {\n random.randomFillSync(\n buffer.buffer ? buffer.buffer : buffer,\n offset,\n size ?? buffer.byteLength\n );\n return buffer;\n}\n\nexport function randomBytes(size: number): ArrayBuffer;\n\nexport function randomBytes(\n size: number,\n callback: (err: Error | null, buf?: ArrayBuffer) => void\n): void;\n\nexport function randomBytes(\n size: number,\n callback?: (err: Error | null, buf?: ArrayBuffer) => void\n): void | ArrayBuffer {\n const buf = new Buffer(size);\n\n if (callback === undefined) {\n randomFillSync(buf.buffer, 0, size);\n return buf;\n }\n\n randomFill(buf.buffer, 0, size, (error: Error | null) => {\n if (error) {\n callback(error);\n }\n callback(null, buf);\n });\n}\n\nexport const rng = randomBytes;\nexport const pseudoRandomBytes = randomBytes;\nexport const prng = randomBytes;\n\ntype RandomIntCallback = (err: Error | null, value: number) => void;\ntype Task = {\n min: number;\n max: number;\n callback: RandomIntCallback;\n};\n\n// The rest of the file is taken from https://github.com/nodejs/node/blob/master/lib/internal/crypto/random.js\n\n// Largest integer we can read from a buffer.\n// e.g.: Buffer.from(\"ff\".repeat(6), \"hex\").readUIntBE(0, 6);\nconst RAND_MAX = 0xffff_ffff_ffff;\n\n// Cache random data to use in randomInt. The cache size must be evenly\n// divisible by 6 because each attempt to obtain a random int uses 6 bytes.\nconst randomCache = new Buffer(6 * 1024);\nlet randomCacheOffset = randomCache.length;\nlet asyncCacheFillInProgress = false;\nconst asyncCachePendingTasks: Task[] = [];\n\n// Generates an integer in [min, max) range where min is inclusive and max is\n// exclusive.\n\nexport function randomInt(max: number, callback: RandomIntCallback): void;\nexport function randomInt(max: number): number;\nexport function randomInt(\n min: number,\n max: number,\n callback: RandomIntCallback\n): void;\nexport function randomInt(min: number, max: number): number;\nexport function randomInt(\n arg1: number,\n arg2?: number | RandomIntCallback,\n callback?: RandomIntCallback\n): void | number {\n // Detect optional min syntax\n // randomInt(max)\n // randomInt(max, callback)\n let max: number;\n let min: number;\n const minNotSpecified =\n typeof arg2 === 'undefined' || typeof arg2 === 'function';\n\n if (minNotSpecified) {\n callback = arg2 as any as undefined | RandomIntCallback;\n max = arg1;\n min = 0;\n } else {\n min = arg1;\n max = arg2 as any as number;\n }\n\n const isSync = typeof callback === 'undefined';\n if (!Number.isSafeInteger(min)) {\n // todo throw new ERR_INVALID_ARG_TYPE('min', 'a safe integer', min);\n throw 'ERR_INVALID_ARG_TYPE';\n }\n if (!Number.isSafeInteger(max)) {\n // todo throw new ERR_INVALID_ARG_TYPE('max', 'a safe integer', max);\n throw 'ERR_INVALID_ARG_TYPE';\n }\n if (max <= min) {\n /* todo throw new ERR_OUT_OF_RANGE(\n 'max',\n `greater than the value of \"min\" (${min})`,\n max\n );*/\n throw 'ERR_OUT_OF_RANGE';\n }\n\n // First we generate a random int between [0..range)\n const range = max - min;\n\n if (!(range <= RAND_MAX)) {\n /* todo throw new ERR_OUT_OF_RANGE(\n `max${minNotSpecified ? '' : ' - min'}`,\n `<= ${RAND_MAX}`,\n range\n );*/\n throw 'ERR_OUT_OF_RANGE';\n }\n\n // For (x % range) to produce an unbiased value greater than or equal to 0 and\n // less than range, x must be drawn randomly from the set of integers greater\n // than or equal to 0 and less than randLimit.\n const randLimit = RAND_MAX - (RAND_MAX % range);\n\n // If we don't have a callback, or if there is still data in the cache, we can\n // do this synchronously, which is super fast.\n while (isSync || randomCacheOffset < randomCache.length) {\n if (randomCacheOffset === randomCache.length) {\n // This might block the thread for a bit, but we are in sync mode.\n randomFillSync(randomCache);\n randomCacheOffset = 0;\n }\n\n const x = randomCache.readUIntBE(randomCacheOffset, 6);\n randomCacheOffset += 6;\n\n if (x < randLimit) {\n const n = (x % range) + min;\n if (isSync) return n;\n process.nextTick(callback as Function, undefined, n);\n return;\n }\n }\n\n // At this point, we are in async mode with no data in the cache. We cannot\n // simply refill the cache, because another async call to randomInt might\n // already be doing that. Instead, queue this call for when the cache has\n // been refilled.\n if (callback !== undefined) {\n // it is (typescript doesn't know it)\n asyncCachePendingTasks.push({ min, max, callback });\n asyncRefillRandomIntCache();\n }\n}\n\nfunction asyncRefillRandomIntCache() {\n if (asyncCacheFillInProgress) return;\n\n asyncCacheFillInProgress = true;\n randomFill(randomCache, (err) => {\n asyncCacheFillInProgress = false;\n\n const tasks = asyncCachePendingTasks;\n const errorReceiver = err && tasks.shift();\n if (!err) randomCacheOffset = 0;\n\n // Restart all pending tasks. If an error occurred, we only notify a single\n // callback (errorReceiver) about it. This way, every async call to\n // randomInt has a chance of being successful, and it avoids complex\n // exception handling here.\n tasks.splice(0).forEach((task) => {\n randomInt(task.min, task.max, task.callback);\n });\n\n // This is the only call that might throw, and is therefore done at the end.\n if (errorReceiver) errorReceiver.callback(err, 0);\n });\n}\n\n// Really just the Web Crypto API alternative\n// to require('crypto').randomFillSync() with an\n// additional limitation that the input buffer is\n// not allowed to exceed 65536 bytes, and can only\n// be an integer-type TypedArray.\ntype DataType =\n | Int8Array\n | Int16Array\n | Int32Array\n | Uint8Array\n | Uint16Array\n | Uint32Array;\nexport function getRandomValues(data: DataType) {\n if (data.byteLength > 65536) {\n throw new Error('The requested length exceeds 65,536 bytes');\n }\n randomFillSync(data, 0);\n return data;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["random.ts"],"names":["random","NativeQuickCrypto","randomFill","buffer","Error","callback","offset","size","byteLength","then","e","randomFillSync","randomBytes","buf","Buffer","undefined","error","rng","pseudoRandomBytes","prng","RAND_MAX","randomCache","randomCacheOffset","length","asyncCacheFillInProgress","asyncCachePendingTasks","randomInt","arg1","arg2","max","min","minNotSpecified","isSync","Number","isSafeInteger","range","randLimit","x","readUIntBE","n","process","nextTick","push","asyncRefillRandomIntCache","err","tasks","errorReceiver","shift","splice","forEach","task","getRandomValues","data"],"mappings":";;;;;;;;;;;;;AAAA;;AACA;;AACA;;AAEA,MAAMA,MAAM,GAAGC,qCAAkBD,MAAjC;;AAgCO,SAASE,UAAT,CAAoBC,MAApB,EAAuD;AAAA;;AAC5D,MAAI,eAAY,qDAAc,CAA1B,8EAAiC,UAArC,EAAiD;AAC/C,UAAM,IAAIC,KAAJ,CAAU,oCAAV,CAAN;AACD;;AAED,QAAMC,QAAQ,YAAQ,qDAAc,CAAtB,4EAAd;AAKA,MAAIC,MAAc,GAAG,CAArB;AACA,MAAIC,IAAY,GAAGJ,MAAM,CAACK,UAA1B;;AAEA,MAAI,8DAAmB,UAAvB,EAAmC;AACjCF,IAAAA,MAAM,mDAAN;AACAC,IAAAA,IAAI,mDAAJ;AACD;;AAED,MAAI,8DAAmB,UAAvB,EAAmC;AACjCD,IAAAA,MAAM,mDAAN;AACD;;AAEDN,EAAAA,MAAM,CACHE,UADH,CACc,qBAASC,MAAT,IAAmBA,MAAM,CAACA,MAA1B,GAAmCA,MADjD,EACyDG,MADzD,EACiEC,IADjE,EAEGE,IAFH,CAGI,MAAM;AACJJ,IAAAA,QAAQ,CAAC,IAAD,EAAOF,MAAP,CAAR;AACD,GALL,EAMKO,CAAD,IAAc;AACZL,IAAAA,QAAQ,CAACK,CAAD,CAAR;AACD,GARL;AAUD;;AAQM,SAASC,cAAT,CAAwBR,MAAxB,EAAwE;AAAA,MAAnCG,MAAmC,uEAAlB,CAAkB;AAAA,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kB,KAAtB,EAA6B;AAC3B,UAAM,IAAIJ,KAAJ,CAAU,2CAAV,CAAN;AACD;;AACDO,EAAAA,cAAc,CAACyC,IAAD,EAAO,CAAP,CAAd;AACA,SAAOA,IAAP;AACD","sourcesContent":["import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';\nimport { Buffer } from '@craftzdog/react-native-buffer';\nimport { isBuffer } from '../src/Utils';\n\nconst random = NativeQuickCrypto.random;\n\ntype TypedArray =\n | Uint8Array\n | Uint8ClampedArray\n | Uint16Array\n | Uint32Array\n | Int8Array\n | Int16Array\n | Int32Array\n | Float32Array\n | Float64Array;\ntype ArrayBufferView = TypedArray | DataView | ArrayBufferLike | Buffer;\n\nexport function randomFill<T extends ArrayBufferView>(\n buffer: T,\n callback: (err: Error | null, buf: T) => void\n): void;\n\nexport function randomFill<T extends ArrayBufferView>(\n buffer: T,\n offset: number,\n callback: (err: Error | null, buf: T) => void\n): void;\n\nexport function randomFill<T extends ArrayBufferView>(\n buffer: T,\n offset: number,\n size: number,\n callback: (err: Error | null, buf: T) => void\n): void;\n\nexport function randomFill(buffer: any, ...rest: any[]): void {\n if (typeof rest[rest.length - 1] !== 'function') {\n throw new Error('No callback provided to randomDill');\n }\n\n const callback = rest[rest.length - 1] as any as (\n err: Error | null,\n buf?: ArrayBuffer\n ) => void;\n\n let offset: number = 0;\n let size: number = buffer.byteLength;\n\n if (typeof rest[2] === 'function') {\n offset = rest[0];\n size = rest[1];\n }\n\n if (typeof rest[1] === 'function') {\n offset = rest[0];\n }\n\n random\n .randomFill(isBuffer(buffer) ? buffer.buffer : buffer, offset, size)\n .then(\n () => {\n callback(null, buffer);\n },\n (e: Error) => {\n callback(e);\n }\n );\n}\n\nexport function randomFillSync<T extends ArrayBufferView>(\n buffer: T,\n offset?: number,\n size?: number\n): T;\n\nexport function randomFillSync(buffer: any, offset: number = 0, size?: number) {\n random.randomFillSync(\n buffer.buffer ? buffer.buffer : buffer,\n offset,\n size ?? buffer.byteLength\n );\n return buffer;\n}\n\nexport function randomBytes(size: number): ArrayBuffer;\n\nexport function randomBytes(\n size: number,\n callback: (err: Error | null, buf?: ArrayBuffer) => void\n): void;\n\nexport function randomBytes(\n size: number,\n callback?: (err: Error | null, buf?: ArrayBuffer) => void\n): void | ArrayBuffer {\n const buf = new Buffer(size);\n\n if (callback === undefined) {\n randomFillSync(buf.buffer, 0, size);\n return buf;\n }\n\n randomFill(buf.buffer, 0, size, (error: Error | null) => {\n if (error) {\n callback(error);\n }\n callback(null, buf);\n });\n}\n\nexport const rng = randomBytes;\nexport const pseudoRandomBytes = randomBytes;\nexport const prng = randomBytes;\n\ntype RandomIntCallback = (err: Error | null, value: number) => void;\ntype Task = {\n min: number;\n max: number;\n callback: RandomIntCallback;\n};\n\n// The rest of the file is taken from https://github.com/nodejs/node/blob/master/lib/internal/crypto/random.js\n\n// Largest integer we can read from a buffer.\n// e.g.: Buffer.from(\"ff\".repeat(6), \"hex\").readUIntBE(0, 6);\nconst RAND_MAX = 0xffffffffffff;\n\n// Cache random data to use in randomInt. The cache size must be evenly\n// divisible by 6 because each attempt to obtain a random int uses 6 bytes.\nconst randomCache = new Buffer(6 * 1024);\nlet randomCacheOffset = randomCache.length;\nlet asyncCacheFillInProgress = false;\nconst asyncCachePendingTasks: Task[] = [];\n\n// Generates an integer in [min, max) range where min is inclusive and max is\n// exclusive.\n\nexport function randomInt(max: number, callback: RandomIntCallback): void;\nexport function randomInt(max: number): number;\nexport function randomInt(\n min: number,\n max: number,\n callback: RandomIntCallback\n): void;\nexport function randomInt(min: number, max: number): number;\nexport function randomInt(\n arg1: number,\n arg2?: number | RandomIntCallback,\n callback?: RandomIntCallback\n): void | number {\n // Detect optional min syntax\n // randomInt(max)\n // randomInt(max, callback)\n let max: number;\n let min: number;\n const minNotSpecified =\n typeof arg2 === 'undefined' || typeof arg2 === 'function';\n\n if (minNotSpecified) {\n callback = arg2 as any as undefined | RandomIntCallback;\n max = arg1;\n min = 0;\n } else {\n min = arg1;\n max = arg2 as any as number;\n }\n\n const isSync = typeof callback === 'undefined';\n if (!Number.isSafeInteger(min)) {\n // todo throw new ERR_INVALID_ARG_TYPE('min', 'a safe integer', min);\n throw 'ERR_INVALID_ARG_TYPE';\n }\n if (!Number.isSafeInteger(max)) {\n // todo throw new ERR_INVALID_ARG_TYPE('max', 'a safe integer', max);\n throw 'ERR_INVALID_ARG_TYPE';\n }\n if (max <= min) {\n /* todo throw new ERR_OUT_OF_RANGE(\n 'max',\n `greater than the value of \"min\" (${min})`,\n max\n );*/\n throw 'ERR_OUT_OF_RANGE';\n }\n\n // First we generate a random int between [0..range)\n const range = max - min;\n\n if (!(range <= RAND_MAX)) {\n /* todo throw new ERR_OUT_OF_RANGE(\n `max${minNotSpecified ? '' : ' - min'}`,\n `<= ${RAND_MAX}`,\n range\n );*/\n throw 'ERR_OUT_OF_RANGE';\n }\n\n // For (x % range) to produce an unbiased value greater than or equal to 0 and\n // less than range, x must be drawn randomly from the set of integers greater\n // than or equal to 0 and less than randLimit.\n const randLimit = RAND_MAX - (RAND_MAX % range);\n\n // If we don't have a callback, or if there is still data in the cache, we can\n // do this synchronously, which is super fast.\n while (isSync || randomCacheOffset < randomCache.length) {\n if (randomCacheOffset === randomCache.length) {\n // This might block the thread for a bit, but we are in sync mode.\n randomFillSync(randomCache);\n randomCacheOffset = 0;\n }\n\n const x = randomCache.readUIntBE(randomCacheOffset, 6);\n randomCacheOffset += 6;\n\n if (x < randLimit) {\n const n = (x % range) + min;\n if (isSync) return n;\n process.nextTick(callback as Function, undefined, n);\n return;\n }\n }\n\n // At this point, we are in async mode with no data in the cache. We cannot\n // simply refill the cache, because another async call to randomInt might\n // already be doing that. Instead, queue this call for when the cache has\n // been refilled.\n if (callback !== undefined) {\n // it is (typescript doesn't know it)\n asyncCachePendingTasks.push({ min, max, callback });\n asyncRefillRandomIntCache();\n }\n}\n\nfunction asyncRefillRandomIntCache() {\n if (asyncCacheFillInProgress) return;\n\n asyncCacheFillInProgress = true;\n randomFill(randomCache, (err) => {\n asyncCacheFillInProgress = false;\n\n const tasks = asyncCachePendingTasks;\n const errorReceiver = err && tasks.shift();\n if (!err) randomCacheOffset = 0;\n\n // Restart all pending tasks. If an error occurred, we only notify a single\n // callback (errorReceiver) about it. This way, every async call to\n // randomInt has a chance of being successful, and it avoids complex\n // exception handling here.\n tasks.splice(0).forEach((task) => {\n randomInt(task.min, task.max, task.callback);\n });\n\n // This is the only call that might throw, and is therefore done at the end.\n if (errorReceiver) errorReceiver.callback(err, 0);\n });\n}\n\n// Really just the Web Crypto API alternative\n// to require('crypto').randomFillSync() with an\n// additional limitation that the input buffer is\n// not allowed to exceed 65536 bytes, and can only\n// be an integer-type TypedArray.\ntype DataType =\n | Int8Array\n | Int16Array\n | Int32Array\n | Uint8Array\n | Uint16Array\n | Uint32Array;\nexport function getRandomValues(data: DataType) {\n if (data.byteLength > 65536) {\n throw new Error('The requested length exceeds 65,536 bytes');\n }\n randomFillSync(data, 0);\n return data;\n}\n"]}
|
package/lib/commonjs/sig.js
CHANGED
|
@@ -8,7 +8,7 @@ exports.createVerify = createVerify;
|
|
|
8
8
|
|
|
9
9
|
var _NativeQuickCrypto = require("./NativeQuickCrypto/NativeQuickCrypto");
|
|
10
10
|
|
|
11
|
-
var
|
|
11
|
+
var _streamBrowserify = _interopRequireDefault(require("stream-browserify"));
|
|
12
12
|
|
|
13
13
|
var _Utils = require("./Utils");
|
|
14
14
|
|
|
@@ -64,7 +64,7 @@ function getIntOption(name, options) {
|
|
|
64
64
|
return undefined;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
class Verify extends
|
|
67
|
+
class Verify extends _streamBrowserify.default.Writable {
|
|
68
68
|
constructor(algorithm, options) {
|
|
69
69
|
super(options);
|
|
70
70
|
|
|
@@ -109,7 +109,7 @@ class Verify extends _stream.default.Writable {
|
|
|
109
109
|
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
class Sign extends
|
|
112
|
+
class Sign extends _streamBrowserify.default.Writable {
|
|
113
113
|
constructor(algorithm, options) {
|
|
114
114
|
super(options);
|
|
115
115
|
|
package/lib/commonjs/sig.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["sig.ts"],"names":["DSASigEnc","createInternalSign","NativeQuickCrypto","createSign","createInternalVerify","createVerify","getPadding","options","getIntOption","getSaltLength","getDSASignatureEncoding","dsaEncoding","kSigEncDER","kSigEncP1363","Error","name","value","undefined","Verify","Stream","Writable","constructor","algorithm","internal","init","_write","chunk","encoding","callback","update","data","verify","signature","format","type","passphrase","rsaPadding","pssSaltLength","dsaSigEnc","ret","Sign","sign","Buffer","from","toString"],"mappings":";;;;;;;;AAAA;;AAEA;;AASA;;AAKA;;;;;;AAZA;AACA;IACKA,S;;WAAAA,S;AAAAA,EAAAA,S,CAAAA,S;AAAAA,EAAAA,S,CAAAA,S;GAAAA,S,KAAAA,S;;AAYL,MAAMC,kBAAkB,GAAGC,qCAAkBC,UAA7C;AACA,MAAMC,oBAAoB,GAAGF,qCAAkBG,YAA/C;;AAEA,SAASC,UAAT,CAAoBC,OAApB,EAAkC;AAChC,SAAOC,YAAY,CAAC,SAAD,EAAYD,OAAZ,CAAnB;AACD;;AAED,SAASE,aAAT,CAAuBF,OAAvB,EAAqC;AACnC,SAAOC,YAAY,CAAC,YAAD,EAAeD,OAAf,CAAnB;AACD;;AAED,SAASG,uBAAT,CAAiCH,OAAjC,EAA+C;AAC7C,MAAI,OAAOA,OAAP,KAAmB,QAAvB,EAAiC;AAC/B,UAAM;AAAEI,MAAAA,WAAW,GAAG;AAAhB,QAA0BJ,OAAhC;AACA,QAAII,WAAW,KAAK,KAApB,EAA2B,OAAOX,SAAS,CAACY,UAAjB,CAA3B,KACK,IAAID,WAAW,KAAK,YAApB,EAAkC,OAAOX,SAAS,CAACa,YAAjB;AACvC,UAAM,IAAIC,KAAJ,CAAW,wBAAuBH,WAAY,uBAA9C,CAAN;AACD;;AAED,SAAOX,SAAS,CAACY,UAAjB;AACD;;AAED,SAASJ,YAAT,CAAsBO,IAAtB,EAAoCR,OAApC,EAAkD;AAChD,QAAMS,KAAK,GAAGT,OAAO,CAACQ,IAAD,CAArB;;AACA,MAAIC,KAAK,KAAKC,SAAd,EAAyB;AACvB,QAAID,KAAK,KAAKA,KAAK,IAAI,CAAvB,EAA0B;AACxB,aAAOA,KAAP;AACD;;AACD,UAAM,IAAIF,KAAJ,CAAW,WAAUC,IAAK,KAAIC,KAAM,wBAApC,CAAN;AACD;;AACD,SAAOC,SAAP;AACD;;AAED,MAAMC,MAAN,SAAqBC,
|
|
1
|
+
{"version":3,"sources":["sig.ts"],"names":["DSASigEnc","createInternalSign","NativeQuickCrypto","createSign","createInternalVerify","createVerify","getPadding","options","getIntOption","getSaltLength","getDSASignatureEncoding","dsaEncoding","kSigEncDER","kSigEncP1363","Error","name","value","undefined","Verify","Stream","Writable","constructor","algorithm","internal","init","_write","chunk","encoding","callback","update","data","verify","signature","format","type","passphrase","rsaPadding","pssSaltLength","dsaSigEnc","ret","Sign","sign","Buffer","from","toString"],"mappings":";;;;;;;;AAAA;;AAEA;;AASA;;AAKA;;;;;;AAZA;AACA;IACKA,S;;WAAAA,S;AAAAA,EAAAA,S,CAAAA,S;AAAAA,EAAAA,S,CAAAA,S;GAAAA,S,KAAAA,S;;AAYL,MAAMC,kBAAkB,GAAGC,qCAAkBC,UAA7C;AACA,MAAMC,oBAAoB,GAAGF,qCAAkBG,YAA/C;;AAEA,SAASC,UAAT,CAAoBC,OAApB,EAAkC;AAChC,SAAOC,YAAY,CAAC,SAAD,EAAYD,OAAZ,CAAnB;AACD;;AAED,SAASE,aAAT,CAAuBF,OAAvB,EAAqC;AACnC,SAAOC,YAAY,CAAC,YAAD,EAAeD,OAAf,CAAnB;AACD;;AAED,SAASG,uBAAT,CAAiCH,OAAjC,EAA+C;AAC7C,MAAI,OAAOA,OAAP,KAAmB,QAAvB,EAAiC;AAC/B,UAAM;AAAEI,MAAAA,WAAW,GAAG;AAAhB,Q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ArB;AACA,WAAO,IAAP;AACD;;AAEDW,EAAAA,IAAI,CACFlC,OADE,EASFoB,QATE,EAUF;AACA,QAAI,CAACpB,OAAL,EAAc;AACZ,YAAM,IAAIO,KAAJ,CAAU,0BAAV,CAAN;AACD;;AAED,UAAM;AAAEgB,MAAAA,IAAF;AAAQG,MAAAA,MAAR;AAAgBC,MAAAA,IAAhB;AAAsBC,MAAAA;AAAtB,QAAqC,6BAAkB5B,OAAlB,CAA3C;AAEA,UAAM6B,UAAU,GAAG9B,UAAU,CAACC,OAAD,CAA7B;AACA,UAAM8B,aAAa,GAAG5B,aAAa,CAACF,OAAD,CAAnC,CARA,CAUA;;AACA,UAAM+B,SAAS,GAAG5B,uBAAuB,CAACH,OAAD,CAAzC;AAEA,UAAMgC,GAAG,GAAG,KAAKhB,QAAL,CAAckB,IAAd,CACVX,IADU,EAEVG,MAFU,EAGVC,IAHU,EAIVC,UAJU,EAKVC,UALU,EAMVC,aANU,EAOVC,SAPU,CAAZ;AAUAX,IAAAA,QAAQ,GAAGA,QAAQ,IAAI,gCAAvB;;AACA,QAAIA,QAAQ,IAAIA,QAAQ,KAAK,QAA7B,EAAuC;AACrC,aAAOe,MAAM,CAACC,IAAP,CAAYJ,GAAZ,EAAiBK,QAAjB,CAA0BjB,QAA1B,CAAP;AACD;;AAED,WAAOe,MAAM,CAACC,IAAP,CAAYJ,GAAZ,CAAP;AACD;;AA3DgC;;AA8D5B,SAASpC,UAAT,CAAoBmB,SAApB,EAAuCf,OAAvC,EAAsD;AAC3D,SAAO,IAAIiC,IAAJ,CAASlB,SAAT,EAAoBf,OAApB,CAAP;AACD;;AAEM,SAASF,YAAT,CAAsBiB,SAAtB,EAAyCf,OAAzC,EAAwD;AAC7D,SAAO,IAAIW,MAAJ,CAAWI,SAAX,EAAsBf,OAAtB,CAAP;AACD","sourcesContent":["import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';\nimport type { InternalSign, InternalVerify } from './NativeQuickCrypto/sig';\nimport Stream from 'stream-browserify';\n\n// TODO(osp) same as publicCipher on node this are defined on C++ and exposed to node\n// Do the same here\nenum DSASigEnc {\n kSigEncDER,\n kSigEncP1363,\n}\n\nimport {\n BinaryLike,\n binaryLikeToArrayBuffer,\n getDefaultEncoding,\n} from './Utils';\nimport { preparePrivateKey, preparePublicOrPrivateKey } from './keys';\n\nconst createInternalSign = NativeQuickCrypto.createSign;\nconst createInternalVerify = NativeQuickCrypto.createVerify;\n\nfunction getPadding(options: any) {\n return getIntOption('padding', options);\n}\n\nfunction getSaltLength(options: any) {\n return getIntOption('saltLength', options);\n}\n\nfunction getDSASignatureEncoding(options: any) {\n if (typeof options === 'object') {\n const { dsaEncoding = 'der' } = options;\n if (dsaEncoding === 'der') return DSASigEnc.kSigEncDER;\n else if (dsaEncoding === 'ieee-p1363') return DSASigEnc.kSigEncP1363;\n throw new Error(`options.dsaEncoding: ${dsaEncoding} not a valid encoding`);\n }\n\n return DSASigEnc.kSigEncDER;\n}\n\nfunction getIntOption(name: string, options: any) {\n const value = options[name];\n if (value !== undefined) {\n if (value === value >> 0) {\n return value;\n }\n throw new Error(`options.${name}: ${value} not a valid int value`);\n }\n return undefined;\n}\n\nclass Verify extends Stream.Writable {\n private internal: InternalVerify;\n constructor(algorithm: string, options: Stream.WritableOptions) {\n super(options);\n this.internal = createInternalVerify();\n this.internal.init(algorithm);\n }\n\n _write(chunk: BinaryLike, encoding: string, callback: () => void) {\n this.update(chunk, encoding);\n callback();\n }\n\n update(data: BinaryLike, encoding?: string) {\n encoding = encoding ?? getDefaultEncoding();\n data = binaryLikeToArrayBuffer(data, encoding);\n this.internal.update(data);\n return this;\n }\n\n verify(\n options: {\n key: string | Buffer;\n format?: string;\n type?: string;\n passphrase?: string;\n padding?: number;\n saltLength?: number;\n },\n signature: BinaryLike\n ): boolean {\n if (!options) {\n throw new Error('Crypto sign key required');\n }\n\n const { data, format, type, passphrase } =\n preparePublicOrPrivateKey(options);\n\n const rsaPadding = getPadding(options);\n const pssSaltLength = getSaltLength(options);\n\n // Options specific to (EC)DSA\n const dsaSigEnc = getDSASignatureEncoding(options);\n\n const ret = this.internal.verify(\n data,\n format,\n type,\n passphrase,\n binaryLikeToArrayBuffer(signature),\n rsaPadding,\n pssSaltLength,\n dsaSigEnc\n );\n\n return ret;\n }\n}\n\nclass Sign extends Stream.Writable {\n private internal: InternalSign;\n constructor(algorithm: string, options: Stream.WritableOptions) {\n super(options);\n this.internal = createInternalSign();\n this.internal.init(algorithm);\n }\n\n _write(chunk: BinaryLike, encoding: string, callback: () => void) {\n this.update(chunk, encoding);\n callback();\n }\n\n update(data: BinaryLike, encoding?: string) {\n encoding = encoding ?? getDefaultEncoding();\n data = binaryLikeToArrayBuffer(data, encoding);\n this.internal.update(data);\n return this;\n }\n\n sign(\n options: {\n key: string | Buffer;\n format?: string;\n type?: string;\n passphrase?: string;\n padding?: number;\n saltLength?: number;\n },\n encoding?: string\n ) {\n if (!options) {\n throw new Error('Crypto sign key required');\n }\n\n const { data, format, type, passphrase } = preparePrivateKey(options);\n\n const rsaPadding = getPadding(options);\n const pssSaltLength = getSaltLength(options);\n\n // Options specific to (EC)DSA\n const dsaSigEnc = getDSASignatureEncoding(options);\n\n const ret = this.internal.sign(\n data,\n format,\n type,\n passphrase,\n rsaPadding,\n pssSaltLength,\n dsaSigEnc\n );\n\n encoding = encoding || getDefaultEncoding();\n if (encoding && encoding !== 'buffer') {\n return Buffer.from(ret).toString(encoding as any);\n }\n\n return Buffer.from(ret);\n }\n}\n\nexport function createSign(algorithm: string, options?: any) {\n return new Sign(algorithm, options);\n}\n\nexport function createVerify(algorithm: string, options?: any) {\n return new Verify(algorithm, options);\n}\n"]}
|
package/lib/module/random.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["random.ts"],"names":["NativeQuickCrypto","Buffer","isBuffer","random","randomFill","buffer","Error","callback","offset","size","byteLength","then","e","randomFillSync","randomBytes","buf","undefined","error","rng","pseudoRandomBytes","prng","RAND_MAX","randomCache","randomCacheOffset","length","asyncCacheFillInProgress","asyncCachePendingTasks","randomInt","arg1","arg2","max","min","minNotSpecified","isSync","Number","isSafeInteger","range","randLimit","x","readUIntBE","n","process","nextTick","push","asyncRefillRandomIntCache","err","tasks","errorReceiver","shift","splice","forEach","task","getRandomValues","data"],"mappings":"AAAA,SAASA,iBAAT,QAAkC,uCAAlC;AACA,SAASC,MAAT,QAAuB,gCAAvB;AACA,SAASC,QAAT,QAAyB,cAAzB;AAEA,MAAMC,MAAM,GAAGH,iBAAiB,CAACG,MAAjC;AAgCA,OAAO,SAASC,UAAT,CAAoBC,MAApB,EAAuD;AAAA;;AAC5D,MAAI,eAAY,qDAAc,CAA1B,8EAAiC,UAArC,EAAiD;AAC/C,UAAM,IAAIC,KAAJ,CAAU,oCAAV,CAAN;AACD;;AAED,QAAMC,QAAQ,YAAQ,qDAAc,CAAtB,4EAAd;AAKA,MAAIC,MAAc,GAAG,CAArB;AACA,MAAIC,IAAY,GAAGJ,MAAM,CAACK,UAA1B;;AAEA,MAAI,8DAAmB,UAAvB,EAAmC;AACjCF,IAAAA,MAAM,mDAAN;AACAC,IAAAA,IAAI,mDAAJ;AACD;;AAED,MAAI,8DAAmB,UAAvB,EAAmC;AACjCD,IAAAA,MAAM,mDAAN;AACD;;AAEDL,EAAAA,MAAM,CACHC,UADH,CACcF,QAAQ,CAACG,MAAD,CAAR,GAAmBA,MAAM,CAACA,MAA1B,GAAmCA,MADjD,EACyDG,MADzD,EACiEC,IADjE,EAEGE,IAFH,CAGI,MAAM;AACJJ,IAAAA,QAAQ,CAAC,IAAD,EAAOF,MAAP,CAAR;AACD,GALL,EAMKO,CAAD,IAAc;AACZL,IAAAA,QAAQ,CAACK,CAAD,CAAR;AACD,GARL;AAUD;AAQD,OAAO,SAASC,cAAT,CAAwBR,MAAxB,EAAwE;AAAA,MAAnCG,MAAmC,uEAAlB,CAAkB;AAAA,MAAfC,IAAe;AAC7EN,EAAAA,MAAM,CAACU,cAAP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C3B,UAAM,IAAIJ,KAAJ,CAAU,2CAAV,CAAN;AACD;;AACDO,EAAAA,cAAc,CAACwC,IAAD,EAAO,CAAP,CAAd;AACA,SAAOA,IAAP;AACD","sourcesContent":["import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';\nimport { Buffer } from '@craftzdog/react-native-buffer';\nimport { isBuffer } from '../src/Utils';\n\nconst random = NativeQuickCrypto.random;\n\ntype TypedArray =\n | Uint8Array\n | Uint8ClampedArray\n | Uint16Array\n | Uint32Array\n | Int8Array\n | Int16Array\n | Int32Array\n | Float32Array\n | Float64Array;\ntype ArrayBufferView = TypedArray | DataView | ArrayBufferLike | Buffer;\n\nexport function randomFill<T extends ArrayBufferView>(\n buffer: T,\n callback: (err: Error | null, buf: T) => void\n): void;\n\nexport function randomFill<T extends ArrayBufferView>(\n buffer: T,\n offset: number,\n callback: (err: Error | null, buf: T) => void\n): void;\n\nexport function randomFill<T extends ArrayBufferView>(\n buffer: T,\n offset: number,\n size: number,\n callback: (err: Error | null, buf: T) => void\n): void;\n\nexport function randomFill(buffer: any, ...rest: any[]): void {\n if (typeof rest[rest.length - 1] !== 'function') {\n throw new Error('No callback provided to randomDill');\n }\n\n const callback = rest[rest.length - 1] as any as (\n err: Error | null,\n buf?: ArrayBuffer\n ) => void;\n\n let offset: number = 0;\n let size: number = buffer.byteLength;\n\n if (typeof rest[2] === 'function') {\n offset = rest[0];\n size = rest[1];\n }\n\n if (typeof rest[1] === 'function') {\n offset = rest[0];\n }\n\n random\n .randomFill(isBuffer(buffer) ? buffer.buffer : buffer, offset, size)\n .then(\n () => {\n callback(null, buffer);\n },\n (e: Error) => {\n callback(e);\n }\n );\n}\n\nexport function randomFillSync<T extends ArrayBufferView>(\n buffer: T,\n offset?: number,\n size?: number\n): T;\n\nexport function randomFillSync(buffer: any, offset: number = 0, size?: number) {\n random.randomFillSync(\n buffer.buffer ? buffer.buffer : buffer,\n offset,\n size ?? buffer.byteLength\n );\n return buffer;\n}\n\nexport function randomBytes(size: number): ArrayBuffer;\n\nexport function randomBytes(\n size: number,\n callback: (err: Error | null, buf?: ArrayBuffer) => void\n): void;\n\nexport function randomBytes(\n size: number,\n callback?: (err: Error | null, buf?: ArrayBuffer) => void\n): void | ArrayBuffer {\n const buf = new Buffer(size);\n\n if (callback === undefined) {\n randomFillSync(buf.buffer, 0, size);\n return buf;\n }\n\n randomFill(buf.buffer, 0, size, (error: Error | null) => {\n if (error) {\n callback(error);\n }\n callback(null, buf);\n });\n}\n\nexport const rng = randomBytes;\nexport const pseudoRandomBytes = randomBytes;\nexport const prng = randomBytes;\n\ntype RandomIntCallback = (err: Error | null, value: number) => void;\ntype Task = {\n min: number;\n max: number;\n callback: RandomIntCallback;\n};\n\n// The rest of the file is taken from https://github.com/nodejs/node/blob/master/lib/internal/crypto/random.js\n\n// Largest integer we can read from a buffer.\n// e.g.: Buffer.from(\"ff\".repeat(6), \"hex\").readUIntBE(0, 6);\nconst RAND_MAX = 0xffff_ffff_ffff;\n\n// Cache random data to use in randomInt. The cache size must be evenly\n// divisible by 6 because each attempt to obtain a random int uses 6 bytes.\nconst randomCache = new Buffer(6 * 1024);\nlet randomCacheOffset = randomCache.length;\nlet asyncCacheFillInProgress = false;\nconst asyncCachePendingTasks: Task[] = [];\n\n// Generates an integer in [min, max) range where min is inclusive and max is\n// exclusive.\n\nexport function randomInt(max: number, callback: RandomIntCallback): void;\nexport function randomInt(max: number): number;\nexport function randomInt(\n min: number,\n max: number,\n callback: RandomIntCallback\n): void;\nexport function randomInt(min: number, max: number): number;\nexport function randomInt(\n arg1: number,\n arg2?: number | RandomIntCallback,\n callback?: RandomIntCallback\n): void | number {\n // Detect optional min syntax\n // randomInt(max)\n // randomInt(max, callback)\n let max: number;\n let min: number;\n const minNotSpecified =\n typeof arg2 === 'undefined' || typeof arg2 === 'function';\n\n if (minNotSpecified) {\n callback = arg2 as any as undefined | RandomIntCallback;\n max = arg1;\n min = 0;\n } else {\n min = arg1;\n max = arg2 as any as number;\n }\n\n const isSync = typeof callback === 'undefined';\n if (!Number.isSafeInteger(min)) {\n // todo throw new ERR_INVALID_ARG_TYPE('min', 'a safe integer', min);\n throw 'ERR_INVALID_ARG_TYPE';\n }\n if (!Number.isSafeInteger(max)) {\n // todo throw new ERR_INVALID_ARG_TYPE('max', 'a safe integer', max);\n throw 'ERR_INVALID_ARG_TYPE';\n }\n if (max <= min) {\n /* todo throw new ERR_OUT_OF_RANGE(\n 'max',\n `greater than the value of \"min\" (${min})`,\n max\n );*/\n throw 'ERR_OUT_OF_RANGE';\n }\n\n // First we generate a random int between [0..range)\n const range = max - min;\n\n if (!(range <= RAND_MAX)) {\n /* todo throw new ERR_OUT_OF_RANGE(\n `max${minNotSpecified ? '' : ' - min'}`,\n `<= ${RAND_MAX}`,\n range\n );*/\n throw 'ERR_OUT_OF_RANGE';\n }\n\n // For (x % range) to produce an unbiased value greater than or equal to 0 and\n // less than range, x must be drawn randomly from the set of integers greater\n // than or equal to 0 and less than randLimit.\n const randLimit = RAND_MAX - (RAND_MAX % range);\n\n // If we don't have a callback, or if there is still data in the cache, we can\n // do this synchronously, which is super fast.\n while (isSync || randomCacheOffset < randomCache.length) {\n if (randomCacheOffset === randomCache.length) {\n // This might block the thread for a bit, but we are in sync mode.\n randomFillSync(randomCache);\n randomCacheOffset = 0;\n }\n\n const x = randomCache.readUIntBE(randomCacheOffset, 6);\n randomCacheOffset += 6;\n\n if (x < randLimit) {\n const n = (x % range) + min;\n if (isSync) return n;\n process.nextTick(callback as Function, undefined, n);\n return;\n }\n }\n\n // At this point, we are in async mode with no data in the cache. We cannot\n // simply refill the cache, because another async call to randomInt might\n // already be doing that. Instead, queue this call for when the cache has\n // been refilled.\n if (callback !== undefined) {\n // it is (typescript doesn't know it)\n asyncCachePendingTasks.push({ min, max, callback });\n asyncRefillRandomIntCache();\n }\n}\n\nfunction asyncRefillRandomIntCache() {\n if (asyncCacheFillInProgress) return;\n\n asyncCacheFillInProgress = true;\n randomFill(randomCache, (err) => {\n asyncCacheFillInProgress = false;\n\n const tasks = asyncCachePendingTasks;\n const errorReceiver = err && tasks.shift();\n if (!err) randomCacheOffset = 0;\n\n // Restart all pending tasks. If an error occurred, we only notify a single\n // callback (errorReceiver) about it. This way, every async call to\n // randomInt has a chance of being successful, and it avoids complex\n // exception handling here.\n tasks.splice(0).forEach((task) => {\n randomInt(task.min, task.max, task.callback);\n });\n\n // This is the only call that might throw, and is therefore done at the end.\n if (errorReceiver) errorReceiver.callback(err, 0);\n });\n}\n\n// Really just the Web Crypto API alternative\n// to require('crypto').randomFillSync() with an\n// additional limitation that the input buffer is\n// not allowed to exceed 65536 bytes, and can only\n// be an integer-type TypedArray.\ntype DataType =\n | Int8Array\n | Int16Array\n | Int32Array\n | Uint8Array\n | Uint16Array\n | Uint32Array;\nexport function getRandomValues(data: DataType) {\n if (data.byteLength > 65536) {\n throw new Error('The requested length exceeds 65,536 bytes');\n }\n randomFillSync(data, 0);\n return data;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["random.ts"],"names":["NativeQuickCrypto","Buffer","isBuffer","random","randomFill","buffer","Error","callback","offset","size","byteLength","then","e","randomFillSync","randomBytes","buf","undefined","error","rng","pseudoRandomBytes","prng","RAND_MAX","randomCache","randomCacheOffset","length","asyncCacheFillInProgress","asyncCachePendingTasks","randomInt","arg1","arg2","max","min","minNotSpecified","isSync","Number","isSafeInteger","range","randLimit","x","readUIntBE","n","process","nextTick","push","asyncRefillRandomIntCache","err","tasks","errorReceiver","shift","splice","forEach","task","getRandomValues","data"],"mappings":"AAAA,SAASA,iBAAT,QAAkC,uCAAlC;AACA,SAASC,MAAT,QAAuB,gCAAvB;AACA,SAASC,QAAT,QAAyB,cAAzB;AAEA,MAAMC,MAAM,GAAGH,iBAAiB,CAACG,MAAjC;AAgCA,OAAO,SAASC,UAAT,CAAoBC,MAApB,EAAuD;AAAA;;AAC5D,MAAI,eAAY,qDAAc,CAA1B,8EAAiC,UAArC,EAAiD;AAC/C,UAAM,IAAIC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wB,GAAG,IAA3B;AACArB,EAAAA,UAAU,CAACkB,WAAD,EAAeuB,GAAD,IAAS;AAC/BpB,IAAAA,wBAAwB,GAAG,KAA3B;AAEA,UAAMqB,KAAK,GAAGpB,sBAAd;AACA,UAAMqB,aAAa,GAAGF,GAAG,IAAIC,KAAK,CAACE,KAAN,EAA7B;AACA,QAAI,CAACH,GAAL,EAAUtB,iBAAiB,GAAG,CAApB,CALqB,CAO/B;AACA;AACA;AACA;;AACAuB,IAAAA,KAAK,CAACG,MAAN,CAAa,CAAb,EAAgBC,OAAhB,CAAyBC,IAAD,IAAU;AAChCxB,MAAAA,SAAS,CAACwB,IAAI,CAACpB,GAAN,EAAWoB,IAAI,CAACrB,GAAhB,EAAqBqB,IAAI,CAAC5C,QAA1B,CAAT;AACD,KAFD,EAX+B,CAe/B;;AACA,QAAIwC,aAAJ,EAAmBA,aAAa,CAACxC,QAAd,CAAuBsC,GAAvB,EAA4B,CAA5B;AACpB,GAjBS,CAAV;AAkBD,C,CAED;AACA;AACA;AACA;AACA;;;AAQA,OAAO,SAASO,eAAT,CAAyBC,IAAzB,EAAyC;AAC9C,MAAIA,IAAI,CAAC3C,UAAL,GAAkB,KAAtB,EAA6B;AAC3B,UAAM,IAAIJ,KAAJ,CAAU,2CAAV,CAAN;AACD;;AACDO,EAAAA,cAAc,CAACwC,IAAD,EAAO,CAAP,CAAd;AACA,SAAOA,IAAP;AACD","sourcesContent":["import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';\nimport { Buffer } from '@craftzdog/react-native-buffer';\nimport { isBuffer } from '../src/Utils';\n\nconst random = NativeQuickCrypto.random;\n\ntype TypedArray =\n | Uint8Array\n | Uint8ClampedArray\n | Uint16Array\n | Uint32Array\n | Int8Array\n | Int16Array\n | Int32Array\n | Float32Array\n | Float64Array;\ntype ArrayBufferView = TypedArray | DataView | ArrayBufferLike | Buffer;\n\nexport function randomFill<T extends ArrayBufferView>(\n buffer: T,\n callback: (err: Error | null, buf: T) => void\n): void;\n\nexport function randomFill<T extends ArrayBufferView>(\n buffer: T,\n offset: number,\n callback: (err: Error | null, buf: T) => void\n): void;\n\nexport function randomFill<T extends ArrayBufferView>(\n buffer: T,\n offset: number,\n size: number,\n callback: (err: Error | null, buf: T) => void\n): void;\n\nexport function randomFill(buffer: any, ...rest: any[]): void {\n if (typeof rest[rest.length - 1] !== 'function') {\n throw new Error('No callback provided to randomDill');\n }\n\n const callback = rest[rest.length - 1] as any as (\n err: Error | null,\n buf?: ArrayBuffer\n ) => void;\n\n let offset: number = 0;\n let size: number = buffer.byteLength;\n\n if (typeof rest[2] === 'function') {\n offset = rest[0];\n size = rest[1];\n }\n\n if (typeof rest[1] === 'function') {\n offset = rest[0];\n }\n\n random\n .randomFill(isBuffer(buffer) ? buffer.buffer : buffer, offset, size)\n .then(\n () => {\n callback(null, buffer);\n },\n (e: Error) => {\n callback(e);\n }\n );\n}\n\nexport function randomFillSync<T extends ArrayBufferView>(\n buffer: T,\n offset?: number,\n size?: number\n): T;\n\nexport function randomFillSync(buffer: any, offset: number = 0, size?: number) {\n random.randomFillSync(\n buffer.buffer ? buffer.buffer : buffer,\n offset,\n size ?? buffer.byteLength\n );\n return buffer;\n}\n\nexport function randomBytes(size: number): ArrayBuffer;\n\nexport function randomBytes(\n size: number,\n callback: (err: Error | null, buf?: ArrayBuffer) => void\n): void;\n\nexport function randomBytes(\n size: number,\n callback?: (err: Error | null, buf?: ArrayBuffer) => void\n): void | ArrayBuffer {\n const buf = new Buffer(size);\n\n if (callback === undefined) {\n randomFillSync(buf.buffer, 0, size);\n return buf;\n }\n\n randomFill(buf.buffer, 0, size, (error: Error | null) => {\n if (error) {\n callback(error);\n }\n callback(null, buf);\n });\n}\n\nexport const rng = randomBytes;\nexport const pseudoRandomBytes = randomBytes;\nexport const prng = randomBytes;\n\ntype RandomIntCallback = (err: Error | null, value: number) => void;\ntype Task = {\n min: number;\n max: number;\n callback: RandomIntCallback;\n};\n\n// The rest of the file is taken from https://github.com/nodejs/node/blob/master/lib/internal/crypto/random.js\n\n// Largest integer we can read from a buffer.\n// e.g.: Buffer.from(\"ff\".repeat(6), \"hex\").readUIntBE(0, 6);\nconst RAND_MAX = 0xffffffffffff;\n\n// Cache random data to use in randomInt. The cache size must be evenly\n// divisible by 6 because each attempt to obtain a random int uses 6 bytes.\nconst randomCache = new Buffer(6 * 1024);\nlet randomCacheOffset = randomCache.length;\nlet asyncCacheFillInProgress = false;\nconst asyncCachePendingTasks: Task[] = [];\n\n// Generates an integer in [min, max) range where min is inclusive and max is\n// exclusive.\n\nexport function randomInt(max: number, callback: RandomIntCallback): void;\nexport function randomInt(max: number): number;\nexport function randomInt(\n min: number,\n max: number,\n callback: RandomIntCallback\n): void;\nexport function randomInt(min: number, max: number): number;\nexport function randomInt(\n arg1: number,\n arg2?: number | RandomIntCallback,\n callback?: RandomIntCallback\n): void | number {\n // Detect optional min syntax\n // randomInt(max)\n // randomInt(max, callback)\n let max: number;\n let min: number;\n const minNotSpecified =\n typeof arg2 === 'undefined' || typeof arg2 === 'function';\n\n if (minNotSpecified) {\n callback = arg2 as any as undefined | RandomIntCallback;\n max = arg1;\n min = 0;\n } else {\n min = arg1;\n max = arg2 as any as number;\n }\n\n const isSync = typeof callback === 'undefined';\n if (!Number.isSafeInteger(min)) {\n // todo throw new ERR_INVALID_ARG_TYPE('min', 'a safe integer', min);\n throw 'ERR_INVALID_ARG_TYPE';\n }\n if (!Number.isSafeInteger(max)) {\n // todo throw new ERR_INVALID_ARG_TYPE('max', 'a safe integer', max);\n throw 'ERR_INVALID_ARG_TYPE';\n }\n if (max <= min) {\n /* todo throw new ERR_OUT_OF_RANGE(\n 'max',\n `greater than the value of \"min\" (${min})`,\n max\n );*/\n throw 'ERR_OUT_OF_RANGE';\n }\n\n // First we generate a random int between [0..range)\n const range = max - min;\n\n if (!(range <= RAND_MAX)) {\n /* todo throw new ERR_OUT_OF_RANGE(\n `max${minNotSpecified ? '' : ' - min'}`,\n `<= ${RAND_MAX}`,\n range\n );*/\n throw 'ERR_OUT_OF_RANGE';\n }\n\n // For (x % range) to produce an unbiased value greater than or equal to 0 and\n // less than range, x must be drawn randomly from the set of integers greater\n // than or equal to 0 and less than randLimit.\n const randLimit = RAND_MAX - (RAND_MAX % range);\n\n // If we don't have a callback, or if there is still data in the cache, we can\n // do this synchronously, which is super fast.\n while (isSync || randomCacheOffset < randomCache.length) {\n if (randomCacheOffset === randomCache.length) {\n // This might block the thread for a bit, but we are in sync mode.\n randomFillSync(randomCache);\n randomCacheOffset = 0;\n }\n\n const x = randomCache.readUIntBE(randomCacheOffset, 6);\n randomCacheOffset += 6;\n\n if (x < randLimit) {\n const n = (x % range) + min;\n if (isSync) return n;\n process.nextTick(callback as Function, undefined, n);\n return;\n }\n }\n\n // At this point, we are in async mode with no data in the cache. We cannot\n // simply refill the cache, because another async call to randomInt might\n // already be doing that. Instead, queue this call for when the cache has\n // been refilled.\n if (callback !== undefined) {\n // it is (typescript doesn't know it)\n asyncCachePendingTasks.push({ min, max, callback });\n asyncRefillRandomIntCache();\n }\n}\n\nfunction asyncRefillRandomIntCache() {\n if (asyncCacheFillInProgress) return;\n\n asyncCacheFillInProgress = true;\n randomFill(randomCache, (err) => {\n asyncCacheFillInProgress = false;\n\n const tasks = asyncCachePendingTasks;\n const errorReceiver = err && tasks.shift();\n if (!err) randomCacheOffset = 0;\n\n // Restart all pending tasks. If an error occurred, we only notify a single\n // callback (errorReceiver) about it. This way, every async call to\n // randomInt has a chance of being successful, and it avoids complex\n // exception handling here.\n tasks.splice(0).forEach((task) => {\n randomInt(task.min, task.max, task.callback);\n });\n\n // This is the only call that might throw, and is therefore done at the end.\n if (errorReceiver) errorReceiver.callback(err, 0);\n });\n}\n\n// Really just the Web Crypto API alternative\n// to require('crypto').randomFillSync() with an\n// additional limitation that the input buffer is\n// not allowed to exceed 65536 bytes, and can only\n// be an integer-type TypedArray.\ntype DataType =\n | Int8Array\n | Int16Array\n | Int32Array\n | Uint8Array\n | Uint16Array\n | Uint32Array;\nexport function getRandomValues(data: DataType) {\n if (data.byteLength > 65536) {\n throw new Error('The requested length exceeds 65,536 bytes');\n }\n randomFillSync(data, 0);\n return data;\n}\n"]}
|
package/lib/module/sig.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
2
2
|
|
|
3
3
|
import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';
|
|
4
|
-
import Stream from 'stream'; // TODO(osp) same as publicCipher on node this are defined on C++ and exposed to node
|
|
4
|
+
import Stream from 'stream-browserify'; // TODO(osp) same as publicCipher on node this are defined on C++ and exposed to node
|
|
5
5
|
// Do the same here
|
|
6
6
|
|
|
7
7
|
var DSASigEnc;
|
package/lib/module/sig.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["sig.ts"],"names":["NativeQuickCrypto","Stream","DSASigEnc","binaryLikeToArrayBuffer","getDefaultEncoding","preparePrivateKey","preparePublicOrPrivateKey","createInternalSign","createSign","createInternalVerify","createVerify","getPadding","options","getIntOption","getSaltLength","getDSASignatureEncoding","dsaEncoding","kSigEncDER","kSigEncP1363","Error","name","value","undefined","Verify","Writable","constructor","algorithm","internal","init","_write","chunk","encoding","callback","update","data","verify","signature","format","type","passphrase","rsaPadding","pssSaltLength","dsaSigEnc","ret","Sign","sign","Buffer","from","toString"],"mappings":";;AAAA,SAASA,iBAAT,QAAkC,uCAAlC;AAEA,OAAOC,MAAP,MAAmB,
|
|
1
|
+
{"version":3,"sources":["sig.ts"],"names":["NativeQuickCrypto","Stream","DSASigEnc","binaryLikeToArrayBuffer","getDefaultEncoding","preparePrivateKey","preparePublicOrPrivateKey","createInternalSign","createSign","createInternalVerify","createVerify","getPadding","options","getIntOption","getSaltLength","getDSASignatureEncoding","dsaEncoding","kSigEncDER","kSigEncP1363","Error","name","value","undefined","Verify","Writable","constructor","algorithm","internal","init","_write","chunk","encoding","callback","update","data","verify","signature","format","type","passphrase","rsaPadding","pssSaltLength","dsaSigEnc","ret","Sign","sign","Buffer","from","toString"],"mappings":";;AAAA,SAASA,iBAAT,QAAkC,uCAAlC;AAEA,OAAOC,MAAP,MAAmB,mBAAnB,C,CAEA;AACA;;IACKC,S;;WAAAA,S;AAAAA,EAAAA,S,CAAAA,S;AAAAA,EAAAA,S,CAAAA,S;GAAAA,S,KAAAA,S;;AAKL,SAEEC,uBAFF,EAGEC,kBAHF,QAIO,SAJP;AAKA,SAASC,iBAAT,EAA4BC,yBAA5B,QAA6D,QAA7D;AAEA,MAAMC,kBAAkB,GAAGP,iBAAiB,CAACQ,UAA7C;AACA,MAAMC,oBAAoB,GAAGT,iBAAiB,CAACU,YAA/C;;AAEA,SAASC,UAAT,CAAoBC,OAApB,EAAkC;AAChC,SAAOC,YAAY,CAAC,SAAD,EAAYD,OAAZ,CAAnB;AACD;;AAED,SAASE,aAAT,CAAuBF,OAAvB,EAAqC;AACnC,SAAOC,YAAY,CAAC,YAAD,EAAeD,OAAf,CAAnB;AACD;;AAED,SAASG,uBAAT,CAAiCH,OAAjC,EAA+C;AAC7C,MAAI,OAAOA,OAAP,KAAmB,QAAvB,EAAiC;AAC/B,UAAM;AAAEI,MAAAA,WAAW,GAAG;AAAhB,QAA0BJ,OAAhC;AACA,QAAII,WAAW,KAAK,KAApB,EAA2B,OAAOd,SAAS,CAACe,UAAjB,CAA3B,KACK,IAAID,WAAW,KAAK,YAApB,EAAkC,OAAOd,SAAS,CAACgB,YAAjB;AACvC,UAAM,IAAIC,KAAJ,CAAW,wBAAuBH,WAAY,uBAA9C,CAAN;AACD;;AAED,SAAOd,SAAS,CAACe,UAAjB;AACD;;AAED,SAASJ,YAAT,CAAsBO,IAAtB,EAAoCR,OAApC,EAAkD;AAChD,QAAMS,KAAK,GAAGT,OAAO,CAACQ,IAAD,CAArB;;AACA,MAAIC,KAAK,KAAK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AACC,OAAD,CAA7B;AACA,UAAM6B,aAAa,GAAG3B,aAAa,CAACF,OAAD,CAAnC,CARA,CAUA;;AACA,UAAM8B,SAAS,GAAG3B,uBAAuB,CAACH,OAAD,CAAzC;AAEA,UAAM+B,GAAG,GAAG,KAAKhB,QAAL,CAAckB,IAAd,CACVX,IADU,EAEVG,MAFU,EAGVC,IAHU,EAIVC,UAJU,EAKVC,UALU,EAMVC,aANU,EAOVC,SAPU,CAAZ;AAUAX,IAAAA,QAAQ,GAAGA,QAAQ,IAAI3B,kBAAkB,EAAzC;;AACA,QAAI2B,QAAQ,IAAIA,QAAQ,KAAK,QAA7B,EAAuC;AACrC,aAAOe,MAAM,CAACC,IAAP,CAAYJ,GAAZ,EAAiBK,QAAjB,CAA0BjB,QAA1B,CAAP;AACD;;AAED,WAAOe,MAAM,CAACC,IAAP,CAAYJ,GAAZ,CAAP;AACD;;AA3DgC;;AA8DnC,OAAO,SAASnC,UAAT,CAAoBkB,SAApB,EAAuCd,OAAvC,EAAsD;AAC3D,SAAO,IAAIgC,IAAJ,CAASlB,SAAT,EAAoBd,OAApB,CAAP;AACD;AAED,OAAO,SAASF,YAAT,CAAsBgB,SAAtB,EAAyCd,OAAzC,EAAwD;AAC7D,SAAO,IAAIW,MAAJ,CAAWG,SAAX,EAAsBd,OAAtB,CAAP;AACD","sourcesContent":["import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';\nimport type { InternalSign, InternalVerify } from './NativeQuickCrypto/sig';\nimport Stream from 'stream-browserify';\n\n// TODO(osp) same as publicCipher on node this are defined on C++ and exposed to node\n// Do the same here\nenum DSASigEnc {\n kSigEncDER,\n kSigEncP1363,\n}\n\nimport {\n BinaryLike,\n binaryLikeToArrayBuffer,\n getDefaultEncoding,\n} from './Utils';\nimport { preparePrivateKey, preparePublicOrPrivateKey } from './keys';\n\nconst createInternalSign = NativeQuickCrypto.createSign;\nconst createInternalVerify = NativeQuickCrypto.createVerify;\n\nfunction getPadding(options: any) {\n return getIntOption('padding', options);\n}\n\nfunction getSaltLength(options: any) {\n return getIntOption('saltLength', options);\n}\n\nfunction getDSASignatureEncoding(options: any) {\n if (typeof options === 'object') {\n const { dsaEncoding = 'der' } = options;\n if (dsaEncoding === 'der') return DSASigEnc.kSigEncDER;\n else if (dsaEncoding === 'ieee-p1363') return DSASigEnc.kSigEncP1363;\n throw new Error(`options.dsaEncoding: ${dsaEncoding} not a valid encoding`);\n }\n\n return DSASigEnc.kSigEncDER;\n}\n\nfunction getIntOption(name: string, options: any) {\n const value = options[name];\n if (value !== undefined) {\n if (value === value >> 0) {\n return value;\n }\n throw new Error(`options.${name}: ${value} not a valid int value`);\n }\n return undefined;\n}\n\nclass Verify extends Stream.Writable {\n private internal: InternalVerify;\n constructor(algorithm: string, options: Stream.WritableOptions) {\n super(options);\n this.internal = createInternalVerify();\n this.internal.init(algorithm);\n }\n\n _write(chunk: BinaryLike, encoding: string, callback: () => void) {\n this.update(chunk, encoding);\n callback();\n }\n\n update(data: BinaryLike, encoding?: string) {\n encoding = encoding ?? getDefaultEncoding();\n data = binaryLikeToArrayBuffer(data, encoding);\n this.internal.update(data);\n return this;\n }\n\n verify(\n options: {\n key: string | Buffer;\n format?: string;\n type?: string;\n passphrase?: string;\n padding?: number;\n saltLength?: number;\n },\n signature: BinaryLike\n ): boolean {\n if (!options) {\n throw new Error('Crypto sign key required');\n }\n\n const { data, format, type, passphrase } =\n preparePublicOrPrivateKey(options);\n\n const rsaPadding = getPadding(options);\n const pssSaltLength = getSaltLength(options);\n\n // Options specific to (EC)DSA\n const dsaSigEnc = getDSASignatureEncoding(options);\n\n const ret = this.internal.verify(\n data,\n format,\n type,\n passphrase,\n binaryLikeToArrayBuffer(signature),\n rsaPadding,\n pssSaltLength,\n dsaSigEnc\n );\n\n return ret;\n }\n}\n\nclass Sign extends Stream.Writable {\n private internal: InternalSign;\n constructor(algorithm: string, options: Stream.WritableOptions) {\n super(options);\n this.internal = createInternalSign();\n this.internal.init(algorithm);\n }\n\n _write(chunk: BinaryLike, encoding: string, callback: () => void) {\n this.update(chunk, encoding);\n callback();\n }\n\n update(data: BinaryLike, encoding?: string) {\n encoding = encoding ?? getDefaultEncoding();\n data = binaryLikeToArrayBuffer(data, encoding);\n this.internal.update(data);\n return this;\n }\n\n sign(\n options: {\n key: string | Buffer;\n format?: string;\n type?: string;\n passphrase?: string;\n padding?: number;\n saltLength?: number;\n },\n encoding?: string\n ) {\n if (!options) {\n throw new Error('Crypto sign key required');\n }\n\n const { data, format, type, passphrase } = preparePrivateKey(options);\n\n const rsaPadding = getPadding(options);\n const pssSaltLength = getSaltLength(options);\n\n // Options specific to (EC)DSA\n const dsaSigEnc = getDSASignatureEncoding(options);\n\n const ret = this.internal.sign(\n data,\n format,\n type,\n passphrase,\n rsaPadding,\n pssSaltLength,\n dsaSigEnc\n );\n\n encoding = encoding || getDefaultEncoding();\n if (encoding && encoding !== 'buffer') {\n return Buffer.from(ret).toString(encoding as any);\n }\n\n return Buffer.from(ret);\n }\n}\n\nexport function createSign(algorithm: string, options?: any) {\n return new Sign(algorithm, options);\n}\n\nexport function createVerify(algorithm: string, options?: any) {\n return new Verify(algorithm, options);\n}\n"]}
|
package/lib/typescript/sig.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-quick-crypto",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.4",
|
|
4
4
|
"description": "A fast implementation of Node's `crypto` module written in C/C++ JSI",
|
|
5
5
|
"main": "lib/commonjs/index",
|
|
6
6
|
"module": "lib/module/index",
|
|
@@ -152,7 +152,7 @@
|
|
|
152
152
|
[
|
|
153
153
|
"typescript",
|
|
154
154
|
{
|
|
155
|
-
"project": "tsconfig.
|
|
155
|
+
"project": "tsconfig.json"
|
|
156
156
|
}
|
|
157
157
|
]
|
|
158
158
|
]
|
|
@@ -162,7 +162,6 @@
|
|
|
162
162
|
"@types/node": "^17.0.31",
|
|
163
163
|
"events": "^3.3.0",
|
|
164
164
|
"react-native-quick-base64": "^2.0.2",
|
|
165
|
-
"stream": "^0.0.2",
|
|
166
165
|
"stream-browserify": "^3.0.0",
|
|
167
166
|
"string_decoder": "^1.3.0",
|
|
168
167
|
"crypto-browserify": "^3.12.0"
|
|
@@ -37,7 +37,7 @@ Pod::Spec.new do |s|
|
|
|
37
37
|
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\" \"$(PODS_ROOT)/boost-for-react-native\" \"$(PODS_ROOT)/glog\" \"${PODS_ROOT}/Headers/Public/React-hermes\" \"${PODS_ROOT}/Headers/Public/hermes-engine\""
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
s.dependency "OpenSSL-Universal"
|
|
40
|
+
s.dependency "OpenSSL-Universal"
|
|
41
41
|
s.dependency "React-Core"
|
|
42
42
|
s.dependency "React"
|
|
43
43
|
s.dependency "React-callinvoker"
|
package/src/random.ts
CHANGED
|
@@ -124,7 +124,7 @@ type Task = {
|
|
|
124
124
|
|
|
125
125
|
// Largest integer we can read from a buffer.
|
|
126
126
|
// e.g.: Buffer.from("ff".repeat(6), "hex").readUIntBE(0, 6);
|
|
127
|
-
const RAND_MAX =
|
|
127
|
+
const RAND_MAX = 0xffffffffffff;
|
|
128
128
|
|
|
129
129
|
// Cache random data to use in randomInt. The cache size must be evenly
|
|
130
130
|
// divisible by 6 because each attempt to obtain a random int uses 6 bytes.
|
package/src/sig.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';
|
|
2
2
|
import type { InternalSign, InternalVerify } from './NativeQuickCrypto/sig';
|
|
3
|
-
import Stream from 'stream';
|
|
3
|
+
import Stream from 'stream-browserify';
|
|
4
4
|
|
|
5
5
|
// TODO(osp) same as publicCipher on node this are defined on C++ and exposed to node
|
|
6
6
|
// Do the same here
|