diff --git a/doc/api/auth/pubkey.example.json b/doc/api/auth/pubkey.example.json new file mode 100644 index 0000000..8e48364 --- /dev/null +++ b/doc/api/auth/pubkey.example.json @@ -0,0 +1,9 @@ +{ + "$schema": "pubkey.zh_CN.json", + "ok": true, + "code": 0, + "result": { + "key": "-----BEGIN PUBLIC KEY-----\nMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEArUh+Nj5HC2ib2ZEZx97n\njI3UqhTBD8deFrARjp80PiWCOP2Og3FIych8aS9FSFgnnivCbRu4L1oUKK9yXeen\nw7YhvOydvCExN5R+r8ArgMSjIlgq2fHxCWuKPZrbFr0TuqTXgNgJG3LDq5zt67K1\njJQ4SH3gqXxiWdr/EYlA73io3YrjG7dWb/EWKFvih6RbHC1WwIH2tcyxZ3rSuzH5\nhuTmov+lx0iIHXlCNQY1rgK5QnAo0MSKWOGV5CFDMlk+YYTNu0punxK1BInzxTK8\njIb8xN+54MGgGPTESs3enDmF1jMAtLePPntW0o54WNc2tN1MY7PYo3vx+/yjb3t6\nI4u+C0O5U1kEV1Z82UP8FixZkTwhpTgfp9O/nZRW5Y/sWONgMq6J/5wju/5RT9LE\n6M0RoFth8BE4K78qG1cS2ZX479EKJ0XUjqbFBCyI1ml2YhaWuDvf0EGYXcq9CEm9\nmWa7zWOBEZxVRx0jXxglG7AQqzBkBX0hCdSuRJJsMBsDmYlLGNY8zvmBMi0RC/H1\nFlXum5p4PWadCNFAi3pbZmj2/HpbH6OQsOHcpIv+vKAW2JNWMW7Mg2cpeH10A4cS\nghx4Ss2oSGIyoEOezQFp0BPquQArEDhr5cCM0sLVrreA0OKrBD/RYZwh6jyfUP0e\nTq8VH9KvovUBz2S9cxFz0ZUCAwEAAQ==\n-----END PUBLIC KEY-----\n", + "generated_time": 1663650244 + } +} diff --git a/doc/api/auth/pubkey.zh_CN.json b/doc/api/auth/pubkey.zh_CN.json new file mode 100644 index 0000000..0487f3b --- /dev/null +++ b/doc/api/auth/pubkey.zh_CN.json @@ -0,0 +1,39 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema", + "$id": "https://github.com/lifegpc/pixiv_downloader/raw/master/doc/api/auth/pubkey.zh_CN.json", + "title": "返回结果", + "type": "object", + "properties": { + "ok": { + "description": "请求是否成功", + "type": "boolean" + }, + "code": { + "description": "错误代码,如请求成功则为0", + "type": "integer" + }, + "msg": { + "description": "错误信息", + "type": "string" + }, + "debug_msg": { + "description": "调试用的错误信息" + }, + "result": { + "description": "请求结果", + "type": "object", + "properties": { + "key": { + "description": "RSA公钥", + "type": "string" + }, + "generated_time": { + "description": "公钥生成时间", + "type": "integer" + } + }, + "required": ["key", "generated_time"] + } + }, + "required": ["ok", "code"] +} diff --git a/src/server/auth/pubkey.rs b/src/server/auth/pubkey.rs index 953a6f6..a134f76 100644 --- a/src/server/auth/pubkey.rs +++ b/src/server/auth/pubkey.rs @@ -1,8 +1,12 @@ use super::super::preclude::*; use crate::ext::{json::ToJson2, try_err::TryErr}; use crate::gettext; +use bytes::BytesMut; use chrono::{DateTime, Utc}; -use openssl::{pkey::Private, rsa::Rsa}; +use openssl::{ + pkey::Private, + rsa::{Padding, Rsa}, +}; pub struct RSAKey { pub key: Rsa, @@ -17,6 +21,23 @@ impl RSAKey { }) } + pub fn decrypt(&self, data: &[u8]) -> Result { + let tosize = data.len(); + let mut buf = BytesMut::with_capacity(tosize); + buf.resize(tosize, 0); + let mut i = 0; + let mut real = 0; + while i < tosize { + let i2 = i + self.key.size() as usize; + real += self + .key + .private_decrypt(&data[i..i2], &mut buf[i..i2], Padding::PKCS1)?; + i = i2; + } + buf.truncate(real); + Ok(buf) + } + pub fn is_too_old(&self) -> bool { self.generated_time < Utc::now() - chrono::Duration::hours(1) }