1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package com.yihu.utils;
- import java.io.UnsupportedEncodingException;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.util.Formatter;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.UUID;
- public class WxConfig
- {
- private static String byteToHex(final byte[] hash) {
- Formatter formatter = new Formatter();
- for (byte b : hash)
- {
- formatter.format("%02x", b);
- }
- String result = formatter.toString();
- formatter.close();
- return result;
- }
- private static String create_nonce_str() {
- return UUID.randomUUID().toString();
- }
- private static String create_timestamp() {
- return Long.toString(System.currentTimeMillis() / 1000);
- }
- public static Map<String, String> getConfig(String jsapi_ticket, String url) {
- Map<String, String> ret = new HashMap<String, String>();
- String nonce_str = create_nonce_str();
- String timestamp = create_timestamp();
- String string1;
- String signature = "";
- string1 = "jsapi_ticket=" + jsapi_ticket +
- "&noncestr=" + nonce_str +
- "×tamp=" + timestamp +
- "&url=" + url;
- try
- {
- MessageDigest crypt = MessageDigest.getInstance("SHA-1");
- crypt.reset();
- crypt.update(string1.getBytes("UTF-8"));
- signature = byteToHex(crypt.digest());
- }
- catch (NoSuchAlgorithmException e)
- {
- e.printStackTrace();
- }
- catch (UnsupportedEncodingException e)
- {
- e.printStackTrace();
- }
- ret.put("url", url);
- ret.put("jsapi_ticket", jsapi_ticket);
- ret.put("nonceStr", nonce_str);
- ret.put("timestamp", timestamp);
- ret.put("signature", signature);
- return ret;
- }
- public static void main(String[] args) {
- String jsapi_ticket = "jsapi_ticket";
- String url = "http://example.com";
- Map<String, String> ret = getConfig(jsapi_ticket, url);
- for (Map.Entry entry : ret.entrySet()) {
- System.out.println(entry.getKey() + ", " + entry.getValue());
- }
- }
- }
|