Skip to content

Instantly share code, notes, and snippets.

@lya79
Last active April 16, 2018 06:38
Show Gist options
  • Select an option

  • Save lya79/d8eee5ab6e4f66d6c3ea0cdbfb23bb52 to your computer and use it in GitHub Desktop.

Select an option

Save lya79/d8eee5ab6e4f66d6c3ea0cdbfb23bb52 to your computer and use it in GitHub Desktop.
SIP 摘要認證範例程式
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Random;
/**
* SIP 摘要認證範例程式
*
* HTTP摘要認證 https://zh.wikipedia.org/wiki/HTTP%E6%91%98%E8%A6%81%E8%AE%A4%E8%AF%81
* MD5訊息摘要演算法 https://zh.wikipedia.org/wiki/MD5
*/
public class DigestAccessAuthentication {
private static MessageDigest messageDigest;
private static final char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
/*
* server端儲存的帳號密碼
*/
private static final String USER_NAME = "abc123";
private static final String PWD = "abc123456";
public static void main(String[] args) {
try {
messageDigest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return;
}
String scheme = "Digest";//使用摘要認證演算法
String realm = "192.168.100.166";//Proxy Server的URI
String nonce = generateNonce();//時間戳記
String algorithm = "MD5";//演算法類型
String sipUri = "sip:192.168.100.166:5060";//UA端的 URI
String request = "REGISTER";//功能類型
String info = "";
info += "scheme:" + scheme + "\n";
info += "realm:" + realm + "\n";
info += "nonce:" + nonce + "\n";
info += "algorithm:" + algorithm + "\n";
info += "sipUri:" + sipUri + "\n";
info += "request:" + request + "\n";
System.out.println(info);
/*
* XXX
* client端輸入的帳號密碼
* 測試時可自行修改
*/
String username = "abc123";
String pwd = "abc123456";
String response = generateResponse(realm, username, pwd, nonce, sipUri, request);
/*
* server端進行驗證帳號密碼
*/
boolean authenticated = doAuthenticate(realm, USER_NAME, PWD, nonce, sipUri, request, response);
System.out.println("authenticated:" + authenticated);
}
public static String generateNonce() {
Date date = new Date();
long time = date.getTime();
Random rand = new Random();
String nonceString = (new Long(time)).toString() + (new Long(rand.nextLong())).toString();
byte mdbytes[] = messageDigest.digest(nonceString.getBytes());
return toHexString(mdbytes);
}
public static String generateResponse(String realm, String username, String pwd, String nonce, String uri,
String requestStr) {
String A1 = username + ":" + realm + ":" + pwd;
String HA1 = toHexString(messageDigest.digest(A1.getBytes()));
String A2 = requestStr.toUpperCase() + ":" + uri;
String HA2 = toHexString(messageDigest.digest(A2.getBytes()));
String response = HA1 + ":" + nonce + ":" + HA2;
return toHexString(messageDigest.digest(response.getBytes()));
}
public static boolean doAuthenticate(String realm, String username, String pwd, String nonce, String uri,
String requestStr, String response) {
String md5Str = generateResponse(realm, username, pwd, nonce, uri, requestStr);
return md5Str.equals(response);
}
public static String toHexString(byte b[]) {
int pos = 0;
char[] c = new char[b.length * 2];
for (int i = 0; i < b.length; i++) {
c[pos++] = HEX[(b[i] >> 4) & 0x0F];
c[pos++] = HEX[b[i] & 0x0f];
}
return new String(c);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment