Skip to content

Commit

Permalink
支持使用File System Access API将生成的密钥保存到本地
Browse files Browse the repository at this point in the history
  • Loading branch information
TransparentLC committed Nov 9, 2024
1 parent 637a466 commit 37244fb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
10 changes: 6 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@
<label>密钥类型</label>
<select v-model="keyType">
<optgroup label="ECC">
<option value="curve25519">Curve25519 (ed25519/cv25519)</option>
<option value="p256">NIST P-256</option>
<option value="p384">NIST P-384</option>
<option value="p521">NIST P-521</option>
<option value="curve25519Legacy">Curve25519 (ed25519/cv25519)</option>
<option value="nistP256">NIST P-256</option>
<option value="nistP384">NIST P-384</option>
<option value="nistP521">NIST P-521</option>
<option value="brainpoolP256r1">Brainpool P-256</option>
<option value="brainpoolP384r1">Brainpool P-384</option>
<option value="brainpoolP512r1">Brainpool P-512</option>
Expand Down Expand Up @@ -143,6 +143,8 @@
<input v-model="notification.ntfy" type="checkbox">算号成功后使用 <a href="https://ntfy.sh/app">ntfy</a> 发送通知 <input v-model="notification.ntfyTopic" type="text" style="width:unset;padding:unset" placeholder="主题名称" pattern="[\-_A-Za-z\d]{1,64}">
<br>
<input v-model="nonstopMode" type="checkbox">不间断算号
<br>
<input v-model="saveToDirectory" type="checkbox">将生成的私钥自动保存到本地 <a @click="setSaveDirectory">选择目录</a> {{ saveToDirectoryHandle ? saveToDirectoryHandle.name : '未设定' }}
</div>
<button @click="toggleKeygen" :class="['btn', 'btn-block', running ? 'btn-error' : 'btn-primary']">{{ running ? '停止算号' : '开始算号' }}</button>
<hr>
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
"preview": "vite preview"
},
"devDependencies": {
"@types/node": "^22.7.4",
"@types/node": "^22.9.0",
"@types/wicg-file-system-access": "^2023.10.5",
"rollup-plugin-visualizer": "^5.12.0",
"typescript": "^5.6.2",
"vite": "^5.4.8",
"typescript": "^5.6.3",
"vite": "^5.4.10",
"vite-plugin-html": "^3.2.2"
},
"dependencies": {
"@gera2ld/tarjs": "^0.3.1",
"openpgp": "^5.11.2",
"openpgp": "^6.0.0",
"petite-vue": "^0.4.1",
"terminal.css": "^0.7.5"
},
Expand Down
28 changes: 23 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const app: {
ntfyTopic: string,
},
nonstopMode: boolean,
saveToDirectory: boolean,
saveToDirectoryHandle?: FileSystemDirectoryHandle,
backTime: number,
estimatedHashCount: bigint,
subkeyCombinerArmoredA: string,
Expand All @@ -60,12 +62,13 @@ const app: {
mounted: () => void,
addUserID: () => void,
patternHelper: () => void,
setSaveDirectory: () => Promise<void>,
showAutoFilter: () => void,
toggleKeygen: () => Promise<void>,
bulkDownload: () => Promise<void>,
subkeyCombine: () => Promise<void>,
} = {
keyType: 'curve25519',
keyType: 'curve25519Legacy',
userIDInput: {
name: '',
email: '',
Expand All @@ -84,6 +87,8 @@ const app: {
ntfyTopic: '',
},
nonstopMode: false,
saveToDirectory: false,
saveToDirectoryHandle: undefined,
get backTime() {
return this.thread * this.iteration;
},
Expand Down Expand Up @@ -129,6 +134,13 @@ const app: {
this.pattern = this.formatFingerprint(('*'.repeat(40 - this.patternLength) + this.patternNumber.repeat(this.patternLength)));
},

async setSaveDirectory() {
if (!window.showDirectoryPicker) {
return alert('你的浏览器不支持 File System Access API。\n参见:https://caniuse.com/native-filesystem-api');
}
this.saveToDirectoryHandle = await window.showDirectoryPicker({ mode: 'readwrite' }).catch(() => undefined);
},

showAutoFilter() {
alert(patternToFilter(this.pattern));
},
Expand Down Expand Up @@ -165,10 +177,10 @@ const app: {
userIDs: this.userID,
};
switch (this.keyType) {
case 'curve25519':
case 'p256':
case 'p384':
case 'p521':
case 'curve25519Legacy':
case 'nistP256':
case 'nistP384':
case 'nistP521':
case 'brainpoolP256r1':
case 'brainpoolP384r1':
case 'brainpoolP512r1':
Expand Down Expand Up @@ -212,6 +224,12 @@ const app: {
}),
});
}
if (this.saveToDirectory && this.saveToDirectoryHandle) {
const fileHandle = await this.saveToDirectoryHandle.getFileHandle(`${generatedKey.privateKey.getFingerprint().toUpperCase()}-sec.asc`, { create: true });
const stream = await fileHandle.createWritable();
await stream.write(new Blob([generatedKey.privateKey.armor()]));
await stream.close();
}
}
} while (this.running && this.nonstopMode);
} catch (err) {
Expand Down

0 comments on commit 37244fb

Please sign in to comment.