|
| 1 | +use base16::encode_lower; |
| 2 | +use crypto::{digest::Digest, sha2::Sha256}; |
| 3 | +use hex::decode; |
| 4 | +use reqwest::blocking::Client; |
| 5 | +use uuid::Uuid; |
| 6 | +use machine_uuid; |
| 7 | + |
| 8 | +use aes::Aes256; |
| 9 | +use block_modes::block_padding::Pkcs7; |
| 10 | +use block_modes::{BlockMode, Cbc}; |
| 11 | + |
| 12 | +const BASE_URL: &str = "https://keyauth.com/api/"; |
| 13 | + |
| 14 | +type Aes256Cbc = Cbc<Aes256, Pkcs7>; |
| 15 | +pub struct KeyauthApi { |
| 16 | + name: String, |
| 17 | + owner_id: String, |
| 18 | + secret: String, |
| 19 | +} |
| 20 | + |
| 21 | +impl KeyauthApi { |
| 22 | + pub fn new(name: String, owner_id: String, secret: String) -> Self { |
| 23 | + Self { |
| 24 | + name: name, |
| 25 | + owner_id: owner_id, |
| 26 | + secret: secret, |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + pub fn init(&self) -> Result<(), String> { |
| 31 | + let session_iv = Uuid::new_v4().to_simple().to_string()[..8].to_string(); |
| 32 | + let mut hasher = Sha256::new(); |
| 33 | + hasher.input(session_iv.as_bytes()); |
| 34 | + let init_iv: String = hasher.result_str(); |
| 35 | + let data = format!( |
| 36 | + "type={}&name={}&ownerid={}&init_iv={}", |
| 37 | + encode_lower(b"init"), |
| 38 | + encode_lower(self.name.as_bytes()), |
| 39 | + encode_lower(self.owner_id.as_bytes()), |
| 40 | + &init_iv |
| 41 | + ); |
| 42 | + |
| 43 | + let req = Self::make_req(data); |
| 44 | + |
| 45 | + let response = Encryption::decrypt(req.text().unwrap(), &self.secret, &init_iv); |
| 46 | + |
| 47 | + if response == "KeyAuth_Disabled".to_string() { |
| 48 | + Err("The program key you tried to use doesn't exist".to_string()) |
| 49 | + } else if response == "KeyAuth_Initialized".to_string() { |
| 50 | + Ok(()) |
| 51 | + } else { |
| 52 | + Err("The program key you tried to use doesn't exist".to_string()) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + pub fn login(&self, key: String, hwid: Option<String>) -> Result<(), String> { |
| 57 | + let hwid = match hwid { |
| 58 | + Some(hwid) => hwid, |
| 59 | + None => Self::get_hwid(), |
| 60 | + }; |
| 61 | + let session_iv = Uuid::new_v4().to_simple().to_string()[..8].to_string(); |
| 62 | + let mut hasher = Sha256::new(); |
| 63 | + hasher.input(session_iv.as_bytes()); |
| 64 | + let init_iv: String = hasher.result_str(); |
| 65 | + |
| 66 | + let data = format!( |
| 67 | + "type={}&key={}&hwid={}&name={}&ownerid={}&init_iv={}", |
| 68 | + encode_lower(b"login"), |
| 69 | + Encryption::encrypt(key, &self.secret, &init_iv), |
| 70 | + Encryption::encrypt(hwid, &self.secret, &init_iv), |
| 71 | + encode_lower(self.name.as_bytes()), |
| 72 | + encode_lower(self.owner_id.as_bytes()), |
| 73 | + &init_iv |
| 74 | + ); |
| 75 | + |
| 76 | + let req = Self::make_req(data); |
| 77 | + |
| 78 | + let response = Encryption::decrypt(req.text().unwrap(), &self.secret, &init_iv); |
| 79 | + if response == "KeyAuth_Valid".to_string() { |
| 80 | + Ok(()) |
| 81 | + } else if response == "KeyAuth_Invalid".to_string() { |
| 82 | + Err("Key not found".to_string()) |
| 83 | + } else if response == "KeyAuth_InvalidHWID".to_string() { |
| 84 | + Err("This computer doesn't match the computer the key is locked to. If you reset your computer, contact the application owner".to_string()) |
| 85 | + } else if response == "KeyAuth_Expired".to_string() { |
| 86 | + Err("This key is expired".to_string()) |
| 87 | + } else { |
| 88 | + Err("Application Failed To Connect. Try again or contact application owner".to_string()) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + fn make_req(data: String) -> reqwest::blocking::Response { |
| 93 | + let client = Client::new(); |
| 94 | + client |
| 95 | + .post(BASE_URL) |
| 96 | + .body(data) |
| 97 | + .header("User-Agent", "KeyAuth") |
| 98 | + .header("content-type", "application/x-www-form-urlencoded") |
| 99 | + .send() |
| 100 | + .unwrap() |
| 101 | + } |
| 102 | + |
| 103 | + fn get_hwid() -> String { |
| 104 | + if cfg!(windows) { |
| 105 | + machine_uuid::get() |
| 106 | + } else { |
| 107 | + "None".into() |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +struct Encryption; |
| 113 | +impl Encryption { |
| 114 | + fn encrypt_string(plain_text: &[u8], key: &[u8], iv: &[u8]) -> String { |
| 115 | + let mut buffer = [0u8; 128]; |
| 116 | + let pos = plain_text.len(); |
| 117 | + buffer[..pos].copy_from_slice(plain_text); |
| 118 | + let cipher = Aes256Cbc::new_var(&key, &iv).unwrap(); |
| 119 | + let ciphertext = cipher.encrypt(&mut buffer, pos).unwrap(); |
| 120 | + encode_lower(ciphertext) |
| 121 | + } |
| 122 | + |
| 123 | + fn decrypt_string(cipher_text: &[u8], key: &[u8], iv: &[u8]) -> Vec<u8> { |
| 124 | + let cipher_text = decode(cipher_text).unwrap(); |
| 125 | + let cipher = Aes256Cbc::new_var(&key, &iv).unwrap(); |
| 126 | + let mut buf = cipher_text; |
| 127 | + cipher.decrypt_vec(&mut buf).unwrap() |
| 128 | + } |
| 129 | + |
| 130 | + fn encrypt(message: String, enc_key: &String, iv: &String) -> String { |
| 131 | + let mut hasher = Sha256::new(); |
| 132 | + hasher.input(enc_key.as_bytes()); |
| 133 | + let _key: String = hasher.result_str()[..32].to_owned(); |
| 134 | + |
| 135 | + let mut hasher = Sha256::new(); |
| 136 | + hasher.input(iv.as_bytes()); |
| 137 | + let _iv: String = hasher.result_str()[..16].to_owned(); |
| 138 | + Encryption::encrypt_string(message.as_bytes(), _key.as_bytes(), _iv.as_bytes()) |
| 139 | + } |
| 140 | + |
| 141 | + fn decrypt(message: String, enc_key: &String, iv: &String) -> String { |
| 142 | + let mut hasher = Sha256::new(); |
| 143 | + hasher.input(enc_key.as_bytes()); |
| 144 | + let _key: String = hasher.result_str()[..32].to_owned(); |
| 145 | + |
| 146 | + let mut hasher = Sha256::new(); |
| 147 | + hasher.input(iv.as_bytes()); |
| 148 | + let _iv: String = hasher.result_str()[..16].to_owned(); |
| 149 | + String::from_utf8(Encryption::decrypt_string( |
| 150 | + message.as_bytes(), |
| 151 | + _key.as_bytes(), |
| 152 | + _iv.as_bytes(), |
| 153 | + )) |
| 154 | + .unwrap() |
| 155 | + } |
| 156 | +} |
0 commit comments