古詩詞大全網 - 藝術簽名 - 如何用java程序加密sha1?

如何用java程序加密sha1?

公共?班級?Sha1?{?

/**?

*?SHA1?安全加密算法?

*?@param?地圖?參數鍵值映射集合?

*?@回歸?

*?@throws?DigestException?

*/?

公共?靜電?字符串?SHA1(地圖& lt字符串,對象& gt?地圖)?摔投?DigestException?{?

//獲取信息匯總?-?參數字典排序字符串?

字符串?解密?=?getOrderByLexicographic(地圖);?

試試?{?

//指定sha1算法?

MessageDigest?消化?=?message digest . getinstance(" SHA-1 ");?

digest . update(decrypt . getbytes());?

//獲取字節數組?

字節?消息摘要[]?=?digest . digest();?

//?創造?妖術?字符串?

StringBuffer?hexString?=?新的?string buffer();?

//?字節數組轉換成?十六進制計數?

為了什麽?(int?我?=?0;?我?& lt?messageDigest.length?i++)?{?

字符串?shaHex?=?integer . tohexstring(message digest[I]?& amp?0x ff);?

如果?(shaHex.length()?& lt?2)?{?

hex string . append(0);?

}?

hex string . append(shaHex);?

}?

回歸?hexString.toString()。toupper case();?

}?接住?(NoSuchAlgorithmException?e)?{?

e . printstacktrace();?

扔?新的?DigestException("錯誤的簽名!");?

}?

}?

/**?

*?獲取字典排序的參數?

*?@param?地圖?參數鍵值映射集合?

*?@回歸?字符串?排序字符串?

*/?

私人?靜電?字符串?getOrderByLexicographic(地圖& lt字符串,對象& gt?地圖){?

回歸?split params(lexico graphic recorder(getParamsName(maps)),maps);?

}?

/**?

*?獲取參數名?鑰匙?

*?@param?地圖?參數鍵值映射集合?

*?@回歸?

*/?

私人?靜電?列表& lt字符串& gt?getParamsName(Map & lt;字符串,對象& gt?地圖){?

列表& lt字符串& gt?paramNames?=?新的?ArrayList & lt字符串& gt();?

對於(圖。Entry & lt字符串,對象& gt?入門?:?maps.entrySet()){?

param names . add(entry . getkey());?

}?

回歸?paramNames?

}?

/**?

*?參數名按字典排序?

*?@param?paramNames?參數名稱列表集?

*?@回歸?列出壹組已排序的參數名?

*/?

私人?靜電?列表& lt字符串& gt?詞典記錄器(List & lt字符串& gt?paramNames){?

collections . sort(param names);?

回歸?paramNames?

}?

/**?

*?拼接排序後的參數名和參數值?

*?@param?paramNames?參數名的排序集?

*?@param?地圖?參數鍵值映射集合?

*?@回歸?字符串?拼接的弦?

*/?

私人?靜電?字符串?split params(List & lt;字符串& gt?paramNames,Map & lt字符串,對象& gt?地圖){?

StringBuilder?paramStr?=?新的?StringBuilder();?

for(字符串?paramName?:?paramNames){?

paramstr . append(param name);?

對於(圖。Entry & lt字符串,對象& gt?入門?:?maps.entrySet()){?

if(param name . equals(entry . getkey()){?

paramstr . append(string . value of(entry . getvalue()));?

}?

}?

}?

回歸?paramstr . tostring();?

}