CAFECA-IO / iSunFA

Artificial Intelligence in Financial
https://isunfa.com
GNU General Public License v3.0
0 stars 0 forks source link

[FEATURE] - 完成SSS algorithm utils, get public key api #2210

Closed TinyMurky closed 2 months ago

TinyMurky commented 2 months ago

Summary

完成SSS algorithm utils, get public key api

Tasks

Dependencies

No response

Other Dependencies

No response

Additional Notes

No response

TinyMurky commented 2 months ago

Private key現在會切成5個檔案外加一個metadata image image

TinyMurky commented 2 months ago

後端在 company/[companyId]/public_key回傳

{
"powerby": "iSunFA v0.8.0+41",
"success": false,
"code": "500ISF0000",
"message": "Internal service error",
"payload": {
  "key_ops": [
    "encrypt"
  ],
  "ext": true,
  "kty": "RSA",
  "n": "xMhNQganS6Cf9loJ3OyYfFzFx-otFweslYpsREC9BoacRd2v9YX5gU_fijBiOqhPXj4X_NAfpaDJpEKTBqDE--nfR-H0IhI-5sEZXLCi4p2kj56MYVTT7VSHP15zH76I2dimKmoxl-P8L7kjX1JgdvjqB9uG3hYF8l0HDDvZelaoGMD0SEmVv8NvKX5C_lWKrr6JvryeTaLosEkWLpQGpRdN2vAdA8PRpXOSO6wcp8JfqFFVkedJX75OZfpDMrnqiS6e8ywD1RunCs8Nu2E0KycP3HAGg2v4sL0U9wTD69LK5dN-cg9dJzSqej0WaPdrGd5exrfIREfzUR4C8-F9CQ",
  "e": "AQAB",
  "alg": "RSA-OAEP-256"
  }
}
TinyMurky commented 2 months ago

經過測試之後,被切割的private_key可以被組回並decrypt

{"level":30,"time":1724750249021,"pid":589937,"hostname":"tinymurky-ThinkPad-T480s","name":"console","msg":"Custom Params: undefined"}
{"level":30,"time":1724750275536,"pid":589937,"hostname":"tinymurky-ThinkPad-T480s","name":"console","msg":"isSame true"}

測試function(不會推pr)

async function verifyKeyPair(publicKey: CryptoKey, privateKey: CryptoKey) {
  try {
    const message = 'test message';

    // 使用公钥加密消息
    const encryptedMessage = await encrypt(message, publicKey);

    // 使用私钥解密消息
    const decryptedMessage = await decrypt(encryptedMessage, privateKey);

    // 验证解密后的消息是否与原始消息匹配
    return decryptedMessage === message;
  } catch (error) {
    /* eslint-disable no-console */
    console.error('Verification failed:', error);
    /* eslint-enable no-console */
    return false;
  }
}

async function handleGetRequest(companyId: number): Promise<{
  payload: JsonWebKey | null;
  statusMessage: string;
}> {
  const statusMessage: string = STATUS_MESSAGE.INTERNAL_SERVICE_ERROR;
  let payload: JsonWebKey | null = null;
  try {
    const publicCryptoKey = await getPublicKeyByCompany(companyId);
    const privateKey = await getPrivateKeyByCompany(companyId);
    if (!publicCryptoKey || !privateKey) {
      throw new Error(STATUS_MESSAGE.RESOURCE_NOT_FOUND);
    }
    /* eslint-disable no-console */
    const isSame = await verifyKeyPair(publicCryptoKey, privateKey);
    console.log('isSame', isSame);
    console.log('publicCryptoKey', publicCryptoKey);
    console.log('privateKey', privateKey);
    /* eslint-enable no-console */
    if (publicCryptoKey) {
      payload = await exportPublicKey(publicCryptoKey);
    } else {
      throw new Error(STATUS_MESSAGE.RESOURCE_NOT_FOUND);
    }
  } catch (error) {
    logger.error(error);
  }
  return {
    payload,
    statusMessage,
  };
}
TinyMurky commented 2 months ago

took 8 hours done