/** * bianque.com * Copyright (C) 2013-2023 All Rights Reserved. */ package com.bqhealth.nethospital.supervise.common.utils; import org.apache.commons.lang3.StringUtils; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * * @author geyin * @version FileAuth.java, v 0.1 2023-06-28 10:27 */ public class FileAuth { private static FileAuth instance; private final SecretKeySpec secretKey; private String accessKey; public FileAuth(String accessKey, String secret) { if (isNullOrEmpty(accessKey) || isNullOrEmpty(secret)) throw new IllegalArgumentException("empty key or secret"); byte[] sk = utf8Bytes(secret); SecretKeySpec secretKeySpec = new SecretKeySpec(sk, "HmacSHA1"); this.accessKey = accessKey; this.secretKey = secretKeySpec; instance = this; } public static FileAuth instance() { if (instance == null) throw new IllegalStateException("FileAuth not setup,please check"); return instance; } private static String encodeToString(byte[] data) { return Base64.encodeToString(data, 10); } public static byte[] decode(String data) { return Base64.decode(data, 10); } private static boolean isNullOrEmpty(String s) { return (s == null || "".equals(s)); } private static byte[] utf8Bytes(String data) { return data.getBytes(StandardCharsets.UTF_8); } public static void main(String[] args) { FileAuth fileAuth = new FileAuth("accessKey", "secret"); String uptk = fileAuth.createUploadToken("other-doc"); System.out.println(uptk); String token = fileAuth.createToken("5dbf93147826c67027c438bf", 3600L); System.out.println(token); System.out.println(fileAuth.createTokens("a,b,c", 3600L)); System.out.println(fileAuth.createTokensByListStr("[\"a\",\"b\"]", 3600L)); System.out.println(fileAuth.createTokensByListStr("[1,2]", 3600L)); } public String getAccessKey() { return this.accessKey; } private Mac createMac() { Mac mac; try { mac = Mac.getInstance("HmacSHA1"); mac.init(this.secretKey); } catch (GeneralSecurityException e) { e.printStackTrace(); throw new IllegalArgumentException(e); } return mac; } public String sign(byte[] data) { Mac mac = createMac(); return encodeToString(mac.doFinal(data)); } public String sign(String data) { return sign(utf8Bytes(data)); } private boolean isTimeExpired(long expireTime) { long nowTime = System.currentTimeMillis() / 1000L; return (nowTime > expireTime); } public String createToken(String fileId, long expires) { if (instance == null) return null; long deadline = System.currentTimeMillis() / 1000L + expires; String sign = sign(fileId + "?" + deadline); return StringUtils.join(new Object[] { Long.valueOf(deadline), this.accessKey, sign }, ":"); } private String createTokens(String fileIds, long expires) { String[] keys = StringUtils.split(fileIds, ','); String[] tokens = new String[keys.length]; for (int i = 0; i < keys.length; i++) tokens[i] = createToken(keys[i], expires); return StringUtils.join((Object[])tokens, ","); } private String createTokensByListStr(String fileIdListStr, long expires) { List fileIdList = JSONUtils.>parse(fileIdListStr, (Class)List.class); List strList = new ArrayList<>(); for (Object o : fileIdList) strList.add(String.valueOf(o)); return JSONUtils.toString(createTokens(strList, expires)); } private List createTokens(List fileIdList, long expires) { List list = new ArrayList<>(); for (String s : fileIdList) list.add(createToken(s, expires)); return list; } public String createUploadToken(String bucket) { Map policy = new HashMap<>(); policy.put("scope", bucket); policy.put("deadline", Long.valueOf(System.currentTimeMillis() / 1000L + 3600L)); return createUploadToken(bucket, policy); } public String createUploadToken(String bucket, long expires) { Map policy = new HashMap<>(); policy.put("scope", bucket); policy.put("deadline", Long.valueOf(System.currentTimeMillis() / 1000L + expires)); return createUploadToken(bucket, policy); } public String createUploadToken(String bucket, Map policy) { String sign = sign(JSONUtils.toString(policy)); return this.accessKey + ':' + sign + ':' + encodeToString(utf8Bytes(JSONUtils.toString(policy))); } }