/*
 * Copyright 2024 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <string.h>

#include <iomanip>
#include <sstream>
#include <string>

#include "rsa2048.h"

static const char *rsaPublicKey = R"(-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArw+9L+6kvFl5vTN5QeSW
PUH8fmDeT+UEqmmleYPZlGzUtVDVx+A7rwrhebm6wkEHTXlED2wAnNsksOYPQt9M
KWTMF+zuv8oWH8OfSzKC1QTTIY74KRJz4zkVHujJ87JcCw0gTiK1KQBebO0JJUWf
wrxnGh/sn3SeuWPbk44DpxfKzOBZDXEiWYGCMdWLRBGsyexj/a+Yhdj2/9yDHOaF
rQrMKRnipImjcZMLzWY5jxCTfoOY0AITM0PYQfIfhsSXNdXjh/DzO/SzQdUsba2f
vynrn0T2pNHB93kxFBzmxuAHlieuECVhLprhGYfEpxlKB4m4yLn9BVjFVbzaP329
UwIDAQAB
-----END PUBLIC KEY-----)";

static const char* rsaPrivateKey = R"(-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEArw+9L+6kvFl5vTN5QeSWPUH8fmDeT+UEqmmleYPZlGzUtVDV
x+A7rwrhebm6wkEHTXlED2wAnNsksOYPQt9MKWTMF+zuv8oWH8OfSzKC1QTTIY74
KRJz4zkVHujJ87JcCw0gTiK1KQBebO0JJUWfwrxnGh/sn3SeuWPbk44DpxfKzOBZ
DXEiWYGCMdWLRBGsyexj/a+Yh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lbNZT5CfR1tiw+dNQ19FFjlLOI6DVmSlRW+iszYQePWnIL7IQMNonU/pxPIuh
zSfDDrQwfNVUfOULSQ25ArnEYoxDVkC9YZlmLSHknedR6CtRB8nZgahSN+OJ3tl7
JECIEQKBgQCSna58O25Ap4khqbo4oAf/mXCZ4TAS4YibblTe4QGR7mvDKxn3xZQ4
91Z0P3qpdghy77Must9kNajoH5nwwQoHtLlaHTxCG+p6iNSwprFrlPifCUhM/GgH
/wVEa5KktoFLMyHbZD23P5ThE6+K96f92QQmHV/Vlzuq+R+o+KOJjw==
-----END RSA PRIVATE KEY-----)";

RSA *loadRsaPublicKeyFromMemory(const std::string &key) {
  BIO *bio = BIO_new_mem_buf(key.c_str(), key.length());
  if (bio == nullptr) {
    return nullptr;
  }

  auto *rsa = PEM_read_bio_RSA_PUBKEY(bio, nullptr, nullptr, nullptr);

  BIO_free(bio);

  return rsa;
}
std::string toHex(const unsigned char *data, size_t length) {
    std::stringstream ss;
    ss << std::hex << std::setfill('0');
    for (size_t i = 0; i < length; ++i) {
        if (i > 0 && i % 16 == 0) {
            ss << "\n    ";
        }
        ss << "0x" << std::setw(2) << static_cast<int>(data[i]);
        if (i % 16 != 15 && i != length - 1) {
            ss << ", ";
        }
    }
    return ss.str();
}
std::vector<unsigned char> encodedPbkdf2Key1 = {
    0xa5, 0x8b, 0x5a, 0x0a, 0xea, 0x96, 0xf6, 0xf4, 0x17, 0x09, 0x5d, 0xb3, 0xad, 0xbb, 0x00, 0x69, 0x39, 0x87, 0x61,
    0xe1, 0xbb, 0xc7, 0x0a, 0xd5, 0x2a, 0x8a, 0x38, 0x53, 0x6d, 0x73, 0xa2, 0x49, 0x21, 0x15, 0xd1, 0xeb, 0x6a, 0x7e,
    0x50, 0xcb, 0x3a, 0x07, 0xf9, 0x80, 0x25, 0x51, 0xa7, 0xa1, 0x37, 0x18, 0x3a, 0x41, 0x57, 0xfd, 0x93, 0x52, 0x21,
    0x10, 0xac, 0x5a, 0x7c, 0x67, 0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e9, 0xec, 0xf8, 0x4a,
    0x72, 0xa9, 0xb0, 0x7f, 0x52, 0x7b, 0xb8, 0x61, 0x7e};
int main() {
    RSA* publicKey = loadRsaPublicKeyFromMemory(rsaPublicKey);
    if (publicKey == NULL) {
      printf("Failed to get RSA public key pair.\n");
      return -1;
    }

    // 获取明文数据并进行加密
    char plainText[] = "Hello, Kylin AI SDK!";
    unsigned char encryptedData[RSA_size(publicKey)];

    int encLength = RSA_public_encrypt(plainText.length(), (const unsigned char *)plainText.c_str(), encryptedData,
                                       publicKey, RSA_PKCS1_OAEP_PADDING);

    if (encLength == -1) {
        assert(0);
        return -1;
    }
    std::string hexEncryptedData = toHex(encryptedData, encLength);
    printf("Encrypted Data (Hex):\n    %s\n", hexEncryptedData.c_str());

    Rsa2048 rsa(rsaPrivateKey);
    auto text = rsa.decrypt(std::vector<unsigned char>(encryptedData, encryptedData + encLength));
    assert(text == plainText);

    return 0;
}