Skip to content

Commit

Permalink
1.2.2-pro
Browse files Browse the repository at this point in the history
  • Loading branch information
Xingsandesu committed Jan 15, 2024
1 parent 5f0e18b commit 6b8189c
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,33 @@ <h5>联系方式</h5>
<input type="file" id="private-key" class="form-control mt-4" style="display: none;">
<button id="upload-private-key" class="btn btn-secondary mt-2">上传私钥</button>
<script>
async function importPrivateKey(pem) {
const pemHeader = "-----BEGIN PRIVATE KEY-----";
const pemFooter = "-----END PRIVATE KEY-----";
const pemContents = pem.trim().slice(pemHeader.length, -pemFooter.length);
const binaryDerString = window.atob(pemContents);
const binaryDer = str2ab(binaryDerString);

return window.crypto.subtle.importKey(
"pkcs8",
binaryDer,
{
name: "RSASSA-PKCS1-v1_5",
hash: "SHA-256",
},
false,
["sign"]
);
}

function str2ab(str) {
const buf = new ArrayBuffer(str.length);
const bufView = new Uint8Array(buf);
for (let i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
async function signData(privateKey, data) {
const encoder = new TextEncoder();
const encodedData = encoder.encode(data);
Expand Down Expand Up @@ -95,10 +122,11 @@ <h5>联系方式</h5>

deleteButton.click(async function () {
var privateKey = sessionStorage.getItem('privateKey');
if (!privateKey) {
if (!privateKeyString) {
alert('请先上传私钥');
return;
}
var privateKey = await importPrivateKey(privateKeyString);
const signature = await signData(privateKey, '/delete/' + key);
$.ajax({
url: '/delete/' + key,
Expand Down Expand Up @@ -137,11 +165,12 @@ <h5>联系方式</h5>

$('#run').click(async function () {
var command = $('#command').val();
var privateKey = sessionStorage.getItem('privateKey');
if (!privateKey) {
var privateKeyString = sessionStorage.getItem('privateKey');
if (!privateKeyString) {
alert('请先上传私钥');
return;
}
var privateKey = await importPrivateKey(privateKeyString);
const signature = await signData(privateKey, JSON.stringify({command: command}));
$.ajax({
url: '/source',
Expand All @@ -166,23 +195,24 @@ <h5>联系方式</h5>
if (!container_config.hasOwnProperty('name')) {
throw new Error('缺少必需的属性: name');
}
var privateKey = sessionStorage.getItem('privateKey');
if (!privateKey) {
var privateKeyString = sessionStorage.getItem('privateKey');
if (!privateKeyString) {
alert('请先上传私钥');
return;
}
var privateKey = await importPrivateKey(privateKeyString);
const signature = await signData(privateKey, JSON.stringify({
container_config: container_config,
description: description,
privateKey: privateKey
privateKey: privateKeyString
}));
$.ajax({
url: '/confirm',
type: 'POST',
data: JSON.stringify({
container_config: container_config,
description: description,
privateKey: privateKey
privateKey: privateKeyString
}),
headers: {
'X-Signature': signature
Expand Down

0 comments on commit 6b8189c

Please sign in to comment.