OppoAuthService.java 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. package com.adx.tencent.oppo.service;
  2. import java.nio.charset.StandardCharsets;
  3. import java.security.MessageDigest;
  4. import java.time.Instant;
  5. import java.util.Base64;
  6. /** OPPO ownerToken 本地生成服务。 */
  7. public class OppoAuthService {
  8. public String getOwnerToken(String ownerId, String apiId, String apiKey) {
  9. if (ownerId == null || ownerId.isBlank()
  10. || apiId == null || apiId.isBlank()
  11. || apiKey == null || apiKey.isBlank()) {
  12. throw new IllegalStateException("OPPO ownerId/apiId/apiKey are required");
  13. }
  14. long now = Instant.now().getEpochSecond();
  15. String sign = sha1(apiId + apiKey + now);
  16. return Base64.getEncoder().encodeToString(
  17. (ownerId + "," + apiId + "," + now + "," + sign).getBytes(StandardCharsets.UTF_8));
  18. }
  19. private static String sha1(String value) {
  20. try {
  21. byte[] digest = MessageDigest.getInstance("SHA-1").digest(value.getBytes(StandardCharsets.UTF_8));
  22. StringBuilder result = new StringBuilder();
  23. for (byte b : digest) result.append(String.format("%02x", b));
  24. return result.toString();
  25. } catch (Exception e) {
  26. throw new IllegalStateException("OPPO token sign failed", e);
  27. }
  28. }
  29. }