You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
=== " :fontawesome-brands-python: Change a Setting"
51
+
``` py
52
+
from js2pysecrets.settings import Settings
53
+
54
+
# Initilize Settings
55
+
settings = Settings()
56
+
57
+
# Accessing bits Variables
58
+
print(settings.bits) # 8
59
+
60
+
# Update bits Variables
61
+
settings.update_defaults(bits=16)
62
+
print(settings.bits) # 16
63
+
64
+
# Empty Update - Reverts all settings to default
65
+
settings.update_defaults()
66
+
print(settings.bits) # 8
67
+
```
68
+
69
+
### ==reset_defaults()==
70
+
71
+
=== " :fontawesome-brands-python: Revert to Defaults"
72
+
``` py
73
+
from js2pysecrets.settings import Settings
74
+
75
+
# Initilize Settings
76
+
settings = Settings()
77
+
78
+
# Accessing bits Variables
79
+
print(settings.bits) # 8
80
+
81
+
# Update bits Variables
82
+
settings.update_defaults(bits=16)
83
+
print(settings.bits) # 16
84
+
85
+
# Empty Update - Reverts all settings to default
86
+
settings.reset_defaults()
87
+
print(settings.bits) # 8
88
+
```
89
+
90
+
### ==get_defaults()==
91
+
92
+
=== " :fontawesome-brands-python: Get the Default Values"
93
+
``` py
94
+
from js2pysecrets.settings import Settings
95
+
96
+
# Initilize Settings
97
+
settings = Settings()
98
+
99
+
# Accessing bits Variables
100
+
print(settings.bits) # 8
101
+
102
+
# Update bits Variables
103
+
settings.update_defaults(bits=16)
104
+
print(settings.bits) # 16
105
+
106
+
# Get the default values
107
+
settings.get_defaults()
108
+
defaults = settings.get_defaults()
109
+
110
+
# Updated variables ARE NOT changed
111
+
print(settings.bits) # 16
112
+
113
+
# New variable contains all the defaults
114
+
print(defaults.bits) # 8
115
+
```
116
+
117
+
### ==get_config()==
118
+
119
+
Returns a subset of the settings. _Used by `getConfig()`_
120
+
121
+
=== " :fontawesome-brands-python: get_config()"
122
+
``` py
123
+
from js2pysecrets.settings import Settings
124
+
125
+
# Initilize Settings
126
+
settings = Settings()
127
+
config = settings.get_config()
128
+
print(config)
129
+
"""
130
+
Config(
131
+
bits=8,
132
+
radix=16,
133
+
maxShares=255,
134
+
hasCSPRNG=False,
135
+
typeCSPRNG=None
136
+
)
137
+
"""
138
+
```
139
+
140
+
141
+
142
+
## Running the JavaScript Commands
2
143
3
144
Since the primary goal of this project is to create a Python implementation of Shamir's Secret Sharing that can interoperates with a JavaScript implementation. This package needs to run the JavaScript commands.
4
145
5
146
While there are a variety of Python libraries to execute JavaScript, they had limitations. Some translate JavaScript into Python. For others, using `require()` was a challange. In the end, this package uses a wrapper to make function calls directly to Node.js.
6
147
148
+
## Node.js Wrapper
149
+
150
+
The Node.js wrapper allows tests to be run against the JavaScript version from Python.
151
+
7
152
??? danger "Warning - The JavaScript wrapper uses the `eval()` function. "
8
153
JavaScript's eval() function is a powerful tool that can execute code stored as a string. However, it also poses a security risk when used improperly. Here are a few reasons why eval() is considered insecure:
9
154
@@ -16,6 +161,8 @@ While there are a variety of Python libraries to execute JavaScript, they had li
16
161
- Debugging difficulties: When bugs or errors occur within code executed by eval(), they can be difficult to diagnose and fix due to the dynamic nature of the function. This can make it challenging for developers to find and fix problems in their code.
17
162
18
163
164
+
165
+
19
166
```{ .yaml .no-copy }
20
167
|-- javascript
21
168
| `-- wrapper.js
@@ -36,12 +183,20 @@ While there are a variety of Python libraries to execute JavaScript, they had li
36
183
secrets.share("aabb", 6, 3)
37
184
```
38
185
39
-
There is a drawback to this approach, each call to Node.js invokes a new process. It easily handles single functions, but another approach is needed to handle subsequent calls.
186
+
## Complications with the wrapper
187
+
188
+
There is a drawback to using the wrapper, each call to Node.js invokes a new process. It easily handles single functions, but another approach is needed to handle subsequent calls.
40
189
41
190
42
191
!!! warning "The Python Wrapper"
43
192
44
193
The Python wrapper __does not__ operate like Javascript, calls are not sequential.
194
+
195
+
As you can see in Python example, using `setRNG('testRandom')` is not persistant.
196
+
197
+
Calling `random()` after `setRNG()` __will not__ use the defined RNG.
198
+
199
+
_While in Node.js, `setRNG()` is persistant and all subsequent calls to `random()` uses the RNG defined._
45
200
46
201
=== " :fontawesome-brands-python: Python"
47
202
@@ -73,12 +228,27 @@ There is a drawback to this approach, each call to Node.js invokes a new process
73
228
secrets.random(8) // '15'
74
229
```
75
230
231
+
### Subsequent Commands
232
+
233
+
The wrapper and calling the JavaScript was built to _either_ execute the command in Node.js __or__ output the command as a string to build a list of commands.
234
+
235
+
This is accomplished by setting the `list` keyword in the command to `True`
236
+
237
+
=== " :fontawesome-brands-python: Python"
238
+
239
+
``` py
240
+
some_func('aaa', 1, 2, 3) # Executes the command in Node.js
The Python wrapper __does not__ operate like Javascript, calls are not sequential.
248
+
After building a series of commands, run them _in order_ on Node.js by passing the list to `chain()`
81
249
250
+
Output for each command can be accessed using the corresponding element returned by `chain()`.
251
+
82
252
=== " :fontawesome-brands-python: Python"
83
253
84
254
``` py
@@ -114,18 +284,28 @@ There is a drawback to this approach, each call to Node.js invokes a new process
114
284
secrets.random(8) // '15'
115
285
secrets.random(8) // '15'
116
286
```
287
+
_Now we can see the Python and Node.js function in similar ways._
288
+
289
+
### testRandom
290
+
291
+
Generating repeatable non-random test bits can be __important__ for cryptographic testing.
292
+
293
+
The wrapper includes the ability to call `setRNG('testRandom')` before indvidual commands _without_ the need of building a list. _Without the need to use `chain()`._
294
+
295
+
This is accomplished by setting the `test` keyword in the command to `True`
117
296
118
297
119
298
!!! tip "Using `testRandom`_without_`chain()`"
120
299
121
-
Since a large percentage of tests only require `testRandom` to be run before the command under test,
300
+
Indvidual commands can use `setRNG('testRandom')` without passing a list to `chain()`.
122
301
123
302
=== " :fontawesome-brands-python: Python"
124
303
125
304
``` py
126
305
import js2pysecrets.node as secrets
127
306
128
307
# Use test keyword to enable fixed pattern for simulated random number generation (RNG)
308
+
129
309
# Outputs should all be the same
130
310
secrets.random(8, test=True) # '15'
131
311
secrets.random(8, test=True) # '15'
@@ -149,5 +329,4 @@ There is a drawback to this approach, each call to Node.js invokes a new process
0 commit comments