-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.html
More file actions
186 lines (114 loc) · 4.05 KB
/
Copy pathindex.html
File metadata and controls
186 lines (114 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<html>
<head>
<!-- <script src="libs/index.js" type="module"></script> -->
<script src="node_modules/argon2-browser/lib/argon2.js"></script>
</head>
<body>
<script>
let dataparty_crypto = null
async function main (){
console.log('wooo!')
dataparty_crypto = await import('./libs/index.js')
console.log(dataparty_crypto)
const phrase = await dataparty_crypto.Routines.generateMnemonic()
console.log('phrase')
console.log('\t', phrase)
let key0 = await dataparty_crypto.Routines.createKey()
console.log('key0', key0)
let key = await dataparty_crypto.Routines.createKeyFromMnemonic(phrase)
console.log('key')
console.log('\t', key)
console.log('### Example password')
const password = 'super-strong-password'
const salt = await dataparty_crypto.Routines.generateSalt()
console.log('salt')
console.log('\t', salt.toString('hex'))
let startMs = Date.now()
let key2 = await dataparty_crypto.Routines.createKeyFromPassword(password, salt)
let endMs = Date.now()
console.log('key2')
console.log(key2)
const deltaMs = endMs - startMs
console.log('time (ms):', (deltaMs/1000) )
console.log('### Example messages')
const alice = await dataparty_crypto.Identity.fromRandomSeed({id:'alice', key:key0})
const bob = await dataparty_crypto.Identity.fromRandomSeed({id:'bob', key:key})
let msg1 = new dataparty_crypto.Message({
msg: {
data: 'hello world'
}
})
console.log('alice', alice)
console.log('bob', bob)
const msg = await msg1.encrypt(bob, bob.toMini())
const msg2 = new dataparty_crypto.Message(msg)
const output = await msg2.decrypt(bob)
console.log('\tencrypted message', msg1)
console.log('\tdecrypted message', msg2)
console.log(`alice read: ${JSON.stringify(output,null,2)}`)
console.log('sender', msg2.sender)
console.log('### Example signatures')
const aliceCopy = await dataparty_crypto.Identity.fromRandomSeed(alice.toJSON())
const signedMsg = await alice.sign({a:'hello world'})
console.log('signedMsg', signedMsg)
const signedMsgCopy = new dataparty_crypto.Message(signedMsg)
const verified = await aliceCopy.verify(signedMsgCopy)
console.log('verified?', verified)
console.log('message -', signedMsgCopy.msg)
console.log('sender', signedMsgCopy.sender)
await signedMsgCopy.assertVerified(aliceCopy)
}
async function argonTest(){
console.log('Argon Test')
const fromHexString = (hexString) =>
Uint8Array.from(hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
const salt = await dataparty_crypto.Routines.generateSalt()
console.log('\t','salt', salt.toString('hex'))
console.log(salt)
console.log(argon2)
const key = await dataparty_crypto.Routines.createKeyFromPasswordArgon2(
argon2,
"supersecretpassword123",
salt
)
console.log(key)
}
async function powTest(){
console.log('Proof-of-Work test')
let startMs = Date.now()
let input = 'these are words - '+startMs
let work = await dataparty_crypto.Routines.solveProofOfWork(
input, argon2,
{ timeCost: 3, memoryCost: 1024*8, parallelism: 1, complexity: 9 }
)
let endMs = Date.now()
console.log('the work')
console.log('\t', work)
const deltaMs = endMs - startMs
console.log('solved in (seconds):', (deltaMs/1000) )
console.log('verifying . . . ')
let startMs2 = Date.now()
let verified = await dataparty_crypto.Routines.verifyProofOfWork(
input,
work,
argon2,
{ timeCost: 3, memoryCost: 1024*8, parallelism: 1, complexity: 9 }
)
let endMs2 = Date.now()
let deltaMs2 = endMs2 - startMs2
console.log('verified in (seconds):', (deltaMs2/1000))
console.log('verification:', verified)
}
main().catch(err=>{
console.log('ERROR - we crashed')
console.log(err)
})
.then(()=>{
return argonTest()
})
.then(()=>{
return powTest()
})
</script>
</body>
</html>