需要的自行拿走有问题留言,感谢(可乐🥤)大佬帮忙~
PHP:
function hashMiaoSpeed($token, $request) {
$buildTokens = array_merge([$token], explode("|", trim(BUILDTOKEN)));
$hasher = hash_init("sha512");
hash_update($hasher, $request);
foreach ($buildTokens as $t) {
if ($t == "") {
$t = "SOME_TOKEN";
}
$copy = hash_copy($hasher);
hash_update($copy, $t);
hash_update($copy, hash_final($hasher, true));
$hasher = $copy;
}
$hash = hash_final($hasher, true);
$hashUrlSafe = strtr(base64_encode($hash), '+/', '-_');
return $hashUrlSafe;
}
Python:
import hashlib
import base64
def hash_miao_speed(token, request):
build_tokens = [token] + BUILDTOKEN.strip().split("|")
hasher = hashlib.sha512()
hasher.update(request.encode())
for t in build_tokens:
if t == "":
# unsafe, please make sure not to let token segment be empty
t = "SOME_TOKEN"
copy = hasher.copy()
copy.update(t.encode())
copy.update(hasher.digest())
hasher = copy
hash_bytes = hasher.digest()
hash_url_safe = base64.urlsafe_b64encode(hash_bytes).decode().replace("+", "-").replace("/", "_")
return hash_url_safe
# 示例用法
token = "123"
request = "456"
result = hash_miao_speed(token, request)
print(result)
Rust:
Cargo.toml
[dependencies]
sha2 = "0.10.6"
base64 = "0.13.0"
Code:
use sha2::{Digest, Sha512};
use base64::URL_SAFE;
fn hash_miao_speed(token: &str, request: &str) -> String {
let mut build_tokens = vec![token.to_owned()];
build_tokens.extend(BUILDTOKEN.split("|").map(|s| s.to_owned()));
let mut hasher = Sha512::new();
hasher.update(request);
for t in build_tokens {
let mut copy = hasher.clone();
copy.update(t.as_bytes());
copy.update(hasher.finalize().as_slice());
hasher = copy;
}
let hash_bytes = hasher.finalize();
let hash_url_safe = base64::encode_config(hash_bytes, URL_SAFE);
hash_url_safe
}
// 示例用法
fn main() {
let token = "123";
let request = "456";
let result = hash_miao_speed(token, request);
println!("{}", result);
}
Java
注:java版本要11以上,不然可能会有语法错误
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
public class Main {
public static void main(String[] args) {
String token = "123";
String request = "456";
String result = hashMiaoSpeed(token, request);
System.out.println(result);
}
public static String hashMiaoSpeed(String token, String request) {
List<String> buildTokens = new ArrayList<>();
buildTokens.add(token);
buildTokens.addAll( getPriToken() );
MessageDigest hasher;
try {
hasher = MessageDigest.getInstance("SHA-512");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return "";
}
hasher.update(request.getBytes(StandardCharsets.UTF_8));
for (String t : buildTokens) {
if (t.isEmpty()) {
t = "SOME_TOKEN";
}
MessageDigest copy = cloneMessageDigest(hasher);
copy.update(t.getBytes(StandardCharsets.UTF_8));
copy.update(hasher.digest());
hasher = copy;
}
byte[] hashBytes = hasher.digest();
String hashUrlSafe = Base64.getUrlEncoder().encodeToString(hashBytes)
.replace("+", "-")
.replace("/", "_");
return hashUrlSafe;
}
private static List<String> getPriToken(){
return List.of( StringUtils.split("aa|bb|cc|dd|ee", "|") );
}
private static MessageDigest cloneMessageDigest(MessageDigest messageDigest) {
try {
return (MessageDigest) messageDigest.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
}