You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.0 KiB
80 lines
2.0 KiB
#include <iostream>
|
|
#include <string>
|
|
#include <boost/asio.hpp>
|
|
// 本示例中对合规代码进行了注释
|
|
|
|
// 假设有一个函数去异步验证用户名和密码
|
|
/*void verify_credentials(const std::string& username, const std::string& password) {
|
|
// 如果验证成功,则执行相应操作
|
|
if (is_valid(username, password)) {
|
|
std::cout << "Authentication successful." << std::endl;
|
|
} else {
|
|
std::cout << "Invalid credentials." << std::endl;
|
|
}
|
|
}*/
|
|
|
|
int main() {
|
|
/*std::string user_name, pass_word;
|
|
std::cout << "Enter your username: ";
|
|
std::cin >> user_name;
|
|
std::cout << "Enter your password: ";
|
|
std::getline(std::cin, pass_word);
|
|
|
|
// 调用验证函数
|
|
verify_credentials(user_name, pass_word);*/
|
|
|
|
return 0;
|
|
}
|
|
|
|
#include <openssl/x509.h>
|
|
#include <openssl/pem.h>
|
|
#include <openssl/ssl.h>
|
|
#include <openssl/err.h>
|
|
|
|
bool verifyCertificate(const std::string& certPath) {
|
|
// 初始化OpenSSL库
|
|
/*SSL_library_init();
|
|
OpenSSL_add_all_algorithms();
|
|
ERR_load_crypto_strings();
|
|
|
|
// 加载证书文件
|
|
FILE* certFile = fopen(certPath.c_str(), "r");
|
|
if (!certFile) {
|
|
perror("Failed to open certificate file");
|
|
return false;
|
|
}
|
|
|
|
X509* x509Cert = PEM_read_X509(certFile, NULL, NULL, NULL);
|
|
fclose(certFile);
|
|
|
|
if (!x509Cert) {
|
|
ERR_print_errors_fp(stderr);
|
|
return false;
|
|
}
|
|
|
|
// 这里只做简单的证书有效性检查,真实场景下需要构建完整的信任链并验证到根CA
|
|
int result = X509_verify(x509Cert, NULL);
|
|
|
|
|
|
// 释放资源
|
|
X509_free(x509Cert);
|
|
|
|
// 返回验证结果
|
|
return (result == 1);*/
|
|
}
|
|
|
|
int main2() {
|
|
const std::string certPath = "/path/to/certificate.pem";
|
|
bool isVerified = verifyCertificate(certPath);
|
|
if (isVerified) {
|
|
std::cout << "The certificate has been verified successfully." << std::endl;
|
|
} else {
|
|
std::cout << "Failed to verify the certificate." << std::endl;
|
|
}
|
|
|
|
// 清理OpenSSL库
|
|
EVP_cleanup();
|
|
ERR_free_strings();
|
|
|
|
return 0;
|
|
} |