1
1
import * as dilithium from '../utilities/cryptoMiddleware/dilithium.js' ;
2
2
import * as ed25519 from '../utilities/cryptoMiddleware/ed25519.js' ;
3
+ import {
4
+ currentPath ,
5
+ hasDot ,
6
+ isBuffer ,
7
+ isString
8
+ } from '@universalweb/acid' ;
3
9
import { decode , encode } from '#utilities/serialize' ;
4
- import { blake3Hash } from '../utilities/cryptoMiddleware/blake3.js' ;
10
+ import { keychainGet , keychainSave } from '../utilities/certificate/keychain.js' ;
11
+ import { read , readStructured , write } from '../utilities/file.js' ;
12
+ import { blake3 } from '@noble/hashes/blake3' ;
5
13
import { currentCertificateVersion } from '../defaults.js' ;
6
- import { isBuffer } from '@universalweb/acid' ;
7
- import { write } from '../utilities/file.js' ;
14
+ import { x25519_kyber768Half_xchacha20 } from '../utilities/cryptoMiddleware/x25519_Kyber768Half_xChaCha.js' ;
8
15
const defaultEncryptionAlgorithm = 1 ;
9
16
const defaultSignatureAlgorithm = 1 ;
17
+ const dirname = currentPath ( import . meta) ;
10
18
export class UWProfile {
11
- constructor ( config = { } ) {
19
+ constructor ( config = { } , optionalArg ) {
12
20
if ( config === false ) {
13
21
return this ;
14
22
}
15
- return this . initialize ( config ) ;
23
+ return this . initialize ( config , optionalArg ) ;
16
24
}
17
- async initialize ( config ) {
18
- if ( isBuffer ( config ) ) {
19
- this . importFromBinary ( config ) ;
20
- } else if ( config . publicKey || config . privateKey ) {
25
+ async initialize ( config , optionalArg ) {
26
+ if ( isString ( config ) ) {
27
+ if ( config . includes ( '/' ) || config . includes ( '\\' ) || hasDot ( config ) ) {
28
+ await this . importFile ( config , optionalArg ) ;
29
+ } else {
30
+ await this . importFromKeychain ( config , optionalArg ) ;
31
+ }
32
+ } else if ( isBuffer ( config ) ) {
33
+ await this . importFromBinary ( config , optionalArg ) ;
34
+ } else if ( config ?. publicKey || config ?. privateKey ) {
21
35
const {
22
36
version,
23
37
publicKey,
@@ -37,19 +51,22 @@ export class UWProfile {
37
51
}
38
52
return this ;
39
53
}
40
- async generate ( config ) {
41
- const {
42
- signatureAlgorithm = defaultSignatureAlgorithm ,
43
- encryptionAlgorithm = defaultEncryptionAlgorithm ,
44
- version,
45
- } = config ;
46
- this . version = version || currentCertificateVersion ;
54
+ async generateSignatureKeypair ( ) {
55
+ this . version = currentCertificateVersion ;
47
56
const ed25519NewKeypair = await ed25519 . signatureKeypair ( ) ;
48
57
const dilithiumNewKeypair = await dilithium . signatureKeypair ( ) ;
49
58
console . log ( ed25519NewKeypair . publicKey , ed25519NewKeypair . privateKey ) ;
50
59
this . publicKey = Buffer . concat ( [ ed25519NewKeypair . publicKey , dilithiumNewKeypair . publicKey ] ) ;
51
60
this . privateKey = Buffer . concat ( [ ed25519NewKeypair . privateKey , dilithiumNewKeypair . privateKey ] ) ;
52
61
}
62
+ async generateEncryptionKeypair ( ) {
63
+ const encryptionkeypair = await x25519_kyber768Half_xchacha20 . keypair ( ) ;
64
+ this . encryptionkeypair = encryptionkeypair ;
65
+ }
66
+ async generate ( config ) {
67
+ await this . generateSignatureKeypair ( ) ;
68
+ await this . generateEncryptionKeypair ( ) ;
69
+ }
53
70
get ed25519PublicKey ( ) {
54
71
return this . publicKey . slice ( 0 , 32 ) ;
55
72
}
@@ -79,43 +96,91 @@ export class UWProfile {
79
96
return ( ed25519Verify === dilithiumVerify ) ? ed25519Verify : false ;
80
97
}
81
98
async hash ( message ) {
82
- const hashedMessage = blake3Hash . hash ( message ) ;
99
+ const hashedMessage = blake3 ( message ) ;
83
100
return hashedMessage ;
84
101
}
85
- async importFromBinary ( data ) {
86
- const decodedData = decode ( data ) ;
87
- this . version = decodedData . version ;
88
- this . publicKey = decodedData . publicKey ;
89
- this . privateKey = decodedData . privateKey ;
102
+ async importFromBinary ( data , encryptionKey ) {
103
+ const password = ( isString ( encryptionKey ) ) ? await this . hash ( Buffer . from ( encryptionKey ) ) : encryptionKey ;
104
+ const decodedData = ( password ) ? await this . decryptBinary ( data , password ) : decode ( data ) ;
105
+ await this . importFromObject ( decodedData ) ;
106
+ return this ;
107
+ }
108
+ async importFromObject ( decodedData , encryptionKey ) {
109
+ const password = ( isString ( encryptionKey ) ) ? await this . hash ( Buffer . from ( encryptionKey ) ) : encryptionKey ;
110
+ const data = ( password ) ? await this . decryptBinary ( decodedData . encrypted , password ) : decodedData ;
111
+ this . version = data . version ;
112
+ this . publicKey = data . publicKey ;
113
+ this . privateKey = data . privateKey ;
114
+ this . encryptionKeypair = data . encryptionKeypair ;
90
115
return this ;
91
116
}
92
- async exportAsBinary ( ) {
117
+ async decryptBinary ( encryptedObject , encryptionPassword ) {
118
+ const decrypted = await x25519_kyber768Half_xchacha20 . decrypt ( encryptedObject , encryptionPassword ) ;
119
+ return decode ( decrypted ) ;
120
+ }
121
+ async exportBinary ( encryptionKey ) {
93
122
const data = {
94
123
version : this . version ,
95
124
publicKey : this . publicKey ,
96
125
privateKey : this . privateKey ,
126
+ encryptionKeypair : this . encryptionkeypair
97
127
} ;
98
128
const dataEncoded = encode ( data ) ;
129
+ if ( encryptionKey ) {
130
+ const password = ( isString ( encryptionKey ) ) ? await this . hash ( Buffer . from ( encryptionKey ) ) : encryptionKey ;
131
+ console . log ( password ) ;
132
+ const encryptedData = await x25519_kyber768Half_xchacha20 . encrypt ( dataEncoded , password ) ;
133
+ const encryptedObject = {
134
+ encrypted : encryptedData ,
135
+ } ;
136
+ return encode ( encryptedObject ) ;
137
+ }
99
138
return dataEncoded ;
100
139
}
101
- async saveToFile ( fileLocation , fileName ) {
102
- const dataEncoded = await this . exportAsBinary ( ) ;
140
+ async saveToFile ( fileName , fileLocation , encryptionPassword ) {
141
+ const binaryData = await this . exportBinary ( encryptionPassword ) ;
103
142
const fullPath = `${ fileLocation } /${ fileName } ` ;
104
- write ( fullPath , dataEncoded , 'binary' , true ) ;
143
+ return write ( fullPath , binaryData , 'binary' , true ) ;
144
+ }
145
+ async importFile ( filePath , encryptionPassword ) {
146
+ const data = await readStructured ( filePath ) ;
147
+ if ( data ) {
148
+ return this . importFromObject ( data , encryptionPassword ) ;
149
+ }
150
+ console . log ( 'Error Importing Profile' , filePath ) ;
151
+ return false ;
152
+ }
153
+ async saveToKeychain ( accountName = 'UWProfile' , encryptionPassword ) {
154
+ const binaryData = await this . exportBinary ( encryptionPassword ) ;
155
+ const config = {
156
+ account : this . accountName || accountName ,
157
+ password : binaryData ,
158
+ } ;
159
+ console . log ( 'Profile Size' , binaryData . length ) ;
160
+ return keychainSave ( config ) ;
161
+ }
162
+ async importFromKeychain ( accountName = 'UWProfile' , encryptionPassword ) {
163
+ const keychainObject = await keychainGet ( this . accountName || accountName ) ;
164
+ await this . importFromObject ( keychainObject , encryptionPassword ) ;
105
165
}
106
166
}
107
- export async function uwProfile ( config ) {
108
- const source = new UWProfile ( config ) ;
167
+ export async function uwProfile ( config , optionalArg ) {
168
+ const source = new UWProfile ( config , optionalArg ) ;
109
169
return source ;
110
170
}
111
- const exampleProfileExample = await uwProfile ( ) ;
112
- console . log ( await exampleProfileExample ) ;
113
- console . log ( `Version: ${ exampleProfileExample . version } ` ) ;
114
- console . log ( `Public Key Size: ${ exampleProfileExample . publicKey . length } ` ) ;
115
- console . log ( `Private Key Size: ${ exampleProfileExample . privateKey . length } ` ) ;
116
- console . log ( exampleProfileExample . ed25519PublicKey ) ;
117
- console . log ( exampleProfileExample . ed25519PrivateKey ) ;
118
- const messageExample = Buffer . from ( 'Hello, World!' ) ;
119
- const sig = await exampleProfileExample . sign ( messageExample ) ;
120
- console . log ( sig ) ;
121
- console . log ( await exampleProfileExample . verifySignature ( sig , messageExample ) ) ;
171
+ // const exampleProfileExample = await uwProfile();
172
+ // const encryptionPasswordExample = 'password';
173
+ // console.log(await exampleProfileExample);
174
+ // console.log(`Version: ${exampleProfileExample.version}`);
175
+ // console.log(`Public Key Size: ${exampleProfileExample.publicKey.length}`);
176
+ // console.log(`Private Key Size: ${exampleProfileExample.privateKey.length}`);
177
+ // console.log(exampleProfileExample.ed25519PublicKey);
178
+ // console.log(exampleProfileExample.ed25519PrivateKey);
179
+ // const messageExample = Buffer.from('Hello, World!');
180
+ // const sig = await exampleProfileExample.sign(messageExample);
181
+ // console.log(sig);
182
+ // console.log(await exampleProfileExample.verifySignature(sig, messageExample));
183
+ // await exampleProfileExample.saveToKeychain('exampleUWProfile', encryptionPasswordExample);
184
+ // await exampleProfileExample.saveToFile('profile.cert', `${dirname}/../profiles`, encryptionPasswordExample);
185
+ // console.log(await uwProfile('exampleUWProfile', encryptionPasswordExample));
186
+ // console.log(await uwProfile(`${dirname}/../profiles/profile.cert`, encryptionPasswordExample));
0 commit comments