Rust 零基础,仅靠 GPT4 辅助能否完成全家桶激活码生成(有 Python 基础)
- 稳了!
- 辣鸡 GPT!
0
投票人
Rust 零基础,仅靠 GPT4 辅助能否完成全家桶激活码生成(有 Python 基础)
GPT 生成的生成 license data 方法
#[derive(Serialize)]
struct LicenseProduct {
code: String,
fallbackDate: String,
paidUpTo: String,
extended: bool,
}
#[derive(Serialize)]
struct LicenseData {
licenseId: String,
licenseeName: String,
assigneeName: String,
assigneeEmail: String,
licenseRestriction: String,
checkConcurrentUse: bool,
products: Vec<LicenseProduct>,
metadata: String,
hash: String,
gracePeriodDays: u32,
autoProlongated: bool,
isAutoProlongated: bool,
}
// gen_license_data 函数实现
fn gen_license_data(
ide: Option<HashMap<String, String>>,
plugins: Option<Vec<HashMap<String, String>>>,
license_id: String,
licensee_name: String,
assignee_name: String,
assignee_email: String,
) -> Result<String, String> {
if ide.is_none() && plugins.is_none() {
return Err("The ide and plugins parameters cannot both be null.".to_string());
}
let products = read_products();
let mut data_products = Vec::new();
if let Some(ide_data) = ide {
let ide_code = ide_data.get("code").unwrap();
let ide_expired = ide_data.get("expired").unwrap();
let matched_ide = products
.ide
.into_iter()
.find(|i| i.code == ide_code)
.ok_or("There is no ide code that matches.")?;
for plugin_code in matched_ide.plugin {
let matched_plugin = products
.plugin
.iter()
.find(|p| p.code == plugin_code)
.ok_or("Plugin code not found in products.")?;
data_products.push(LicenseProduct {
code: matched_plugin.code.clone(),
fallbackDate: ide_expired.clone(),
paidUpTo: ide_expired.clone(),
extended: true,
});
}
}
// 添加 plugins 数据
if let Some(plugin_list) = plugins {
for plugin_data in plugin_list {
let plugin_code = plugin_data.get("code").unwrap();
let plugin_expired = plugin_data.get("expired").unwrap();
let matched_plugin = products
.plugin
.iter()
.find(|p| p.code == plugin_code)
.ok_or("Plugin code not found in products.")?;
data_products.push(LicenseProduct {
code: matched_plugin.code.clone(),
fallbackDate: plugin_expired.clone(),
paidUpTo: plugin_expired.clone(),
extended: ide.is_some(),
});
}
}
// 确保 code 唯一
data_products.dedup_by(|a, b| a.code == b.code);
// 构建并返回最终的 json 字符串
let license_data = LicenseData {
licenseId: license_id,
licenseeName: licensee_name,
assigneeName: assignee_name,
assigneeEmail: assignee_email,
licenseRestriction: "".to_string(),
checkConcurrentUse: false,
products: data_products,
metadata: "0120220902PSAN000005".to_string(),
hash: "TRIAL:1234567890".to_string(),
gracePeriodDays: 7,
autoProlongated: false,
isAutoProlongated: false,
};
Ok(serde_json::to_string(&license_data).expect("Failed to serialize license data"))
}
GPT 生成的 license 信息提取 方法
use std::str;
use base64::{decode};
fn extract_jetbra_key(k: &str) {
let k_list: Vec<&str> = k.split('-').collect();
let license_id = k_list[0];
let license_part_base64 = k_list[1];
match decode(license_part_base64) {
Ok(license_part_vec) => {
match str::from_utf8(&license_part_vec) {
Ok(license_part) => {
println!("{}", license_id);
println!("{}", license_part);
},
Err(e) => println!("Invalid UTF-8 sequence: {}", e),
}
},
Err(e) => println!("Base64 decoding error: {}", e),
}
}
变量命名不符合rust规范,java才用驼峰
仓库代码里已经使用#[serde(rename_all(serialize = "camelCase"))]
将struct中snake转camel了
但凡这个readme能写的更详细一点都打算试一下了。
还在写,0基础用GPT写太累了,各种报错,等全部完成了再写 readme
你这要是star数上来了,回头申请jetbrains license,他给过嘛
stars都是浮云,我被干掉3个号了。
你是被人举报了,动了号商们的蛋糕
支持
GPT 生成的生成私钥和证书方法
use openssl::asn1::Asn1Time;
use openssl::pkey::{PKey, Private};
use openssl::rsa::Rsa;
use openssl::x509::{X509Builder, X509NameBuilder, X509};
use std::fmt;
/// Generates an RSA certificate and private key.
///
/// # Arguments
///
/// * `datetime_start` - Tuple representing the start datetime.
/// * `datetime_end` - Tuple representing the end datetime.
/// * `public_exponent` - Public exponent for RSA.
/// * `key_size` - Key size for RSA.
/// * `subject_name` - Subject name for the certificate.
/// * `issuer_name` - Issuer name for the certificate.
///
/// # Returns
///
/// A Result containing a tuple of the private key and certificate in PEM format,
/// or an error message.
pub fn gen_certificate(
datetime_start: (i32, u32, u32, u32, u32, u32),
datetime_end: (i32, u32, u32, u32, u32, u32),
public_exponent: u32,
key_size: u32,
subject_name: &str,
issuer_name: &str,
) -> Result<(Vec<u8>, Vec<u8>), Box<dyn fmt::Error>> {
// Generate RSA private key
let rsa = Rsa::generate_with_e(key_size, public_exponent)?;
let private_key = PKey::from_rsa(rsa)?;
// Get public key
let public_key = PKey::from_rsa(private_key.rsa()?.to_owned())?;
// Create X.509 builder
let mut builder = X509Builder::new()?;
let mut name = X509NameBuilder::new()?;
name.append_entry_by_text("CN", subject_name)?;
let subject_name = name.build();
let mut issuer = X509NameBuilder::new()?;
issuer.append_entry_by_text("CN", issuer_name)?;
let issuer_name = issuer.build();
builder.set_subject_name(&subject_name)?;
builder.set_issuer_name(&issuer_name)?;
builder.set_not_before(&Asn1Time::from_tm(&time_to_tm(datetime_start)?)?)?;
builder.set_not_after(&Asn1Time::from_tm(&time_to_tm(datetime_end)?)?)?;
builder.set_pubkey(&public_key)?;
builder.set_serial_number(&openssl::bn::BigNum::from_u32(rand::random())?.to_asn1_integer()?)?;
// Sign the certificate
builder.sign(&private_key, openssl::hash::MessageDigest::sha256())?;
// Export to PEM
let certificate = builder.build();
let private_key_pem = private_key.private_key_to_pem_pkcs8()?;
let certificate_pem = certificate.to_pem()?;
Ok((private_key_pem, certificate_pem))
}
/// Converts a tuple to a tm structure.
///
/// # Arguments
///
/// * `datetime` - A tuple representing the datetime.
///
/// # Returns
///
/// A Result containing a tm structure, or an error message.
fn time_to_tm(datetime: (i32, u32, u32, u32, u32, u32)) -> Result<time::Tm, Box<dyn fmt::Error>> {
Ok(time::Tm {
tm_year: datetime.0 - 1900, // tm_year is years since 1900
tm_mon: datetime.1 as i32 - 1, // tm_mon is 0-based
tm_mday: datetime.2 as i32,
tm_hour: datetime.3 as i32,
tm_min: datetime.4 as i32,
tm_sec: datetime.5 as i32,
..time::empty_tm()
})
}
这段创建私钥和证书出错在错误的向上传递和time模块乱用
新开个java的分支 我把那个扔进去
我写的那个我是传到自己的git上了
那我把 Readme 链接到你的 git 上
那个我没公开 我直接给你提交pr吧