SHA1加密(Java版)
public class SHA1 {
private static final boolean hexcase = false;
private static final int chrsz = 8;
// 得到字符串SHA-1值的方法
public static String hex_sha1(String s) {
s = (s == null) ? "" : s;
return binb2hex(core_sha1(str2binb(s), s.length() * chrsz));
}
private static String binb2hex(int[] binarray) {
String hex_tab = hexcase ? "0123456789abcdef" : "0123456789abcdef";
String str = "";
for (int i = 0; i < binarray.length * 4; i++) {
char a = (char) hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xf);
char b = (char) hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8)) & 0xf);
str += (new Character(a).toString() + new Character(b).toString());
}
return str;
}
private static String binb2str(int[] bin) {
String str = "";
int mask = (1 << chrsz) - 1;
for (int i = 0; i < bin.length * 32; i += chrsz) {
str += (char) ((bin[i >> 5] >>> (24 - i % 32)) & mask);
}
return str;
}
private static int[] core_sha1(int[] x, int len) {
int size = (len >> 5);
x = strechbinarray(x, size);
x[len >> 5] |= 0x80 << (24 - len % 32);
size = ((len + 64 >> 9) << 4) + 15;
x = strechbinarray(x, size);
x[((len + 64 >> 9) << 4) + 15] = len;
int[] w = new int[80];
int a = 1732584193;
int b = -271733879;
int c = -1732584194;
int d = 271733878;
int e = -1009589776;
for (int i = 0; i < x.length; i += 16) {
int olda = a;
int oldb = b;
int oldc = c;
int oldd = d;
int olde = e;
for (int j = 0; j < 80; j++) {
if (j < 16) {
w[j] = x[i + j];
} else {
w[j] = rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
}
int t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), safe_add(safe_add(e, w[j]), sha1_kt(j)));
e = d;
d = c;
c = rol(b, 30);
b = a;
a = t;
}
a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
e = safe_add(e, olde);
}
int[] retval = new int[5];
retval[0] = a;
retval[1] = b;
retval[2] = c;
retval[3] = d;
retval[4] = e;
return retval;
}
private static int rol(int num, int cnt) {
return (num << cnt) | (num >>> (32 - cnt));
}
private static int safe_add(int x, int y) {
int lsw = (int) (x & 0xffff) + (int) (y & 0xffff);
int msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xffff);
}
private static int sha1_ft(int t, int b, int c, int d) {
if (t < 20)
return (b & c) | ((~b) & d);
if (t < 40)
return b ^ c ^ d;
if (t < 60)
return (b & c) | (b & d) | (c & d);
return b ^ c ^ d;
}
private static int sha1_kt(int t) {
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : (t < 60) ? -1894007588 : -899497514;
}
public static String str_sha1(String s) {
s = (s == null) ? "" : s;
return binb2str(core_sha1(str2binb(s), s.length() * chrsz));
}
private static int[] str2binb(String str) {
str = (str == null) ? "" : str;
int[] tmp = new int[str.length() * chrsz];
int mask = (1 << chrsz) - 1;
for (int i = 0; i < str.length() * chrsz; i += chrsz) {
tmp[i >> 5] |= ((int) (str.charAt(i / chrsz)) & mask) << (24 - i % 32);
}
int len = 0;
for (int i = 0; i < tmp.length && tmp[i] != 0; i++, len++)
;
int[] bin = new int[len];
for (int i = 0; i < len; i++) {
bin[i] = tmp[i];
}
return bin;
}
private static int[] strechbinarray(int[] oldbin, int size) {
int currlen = oldbin.length;
if (currlen >= size + 1) {
return oldbin;
}
int[] newbin = new int[size + 1];
for (int i = 0; i < size; newbin[i] = 0, i++)
;
for (int i = 0; i < currlen; i++) {
newbin[i] = oldbin[i];
}
return newbin;
}
}
测试类:
public static void main(String args[]) {
System.out.println(SHA1.hex_sha1("123456"));
}
SHA1加密(Flex版)
package
{
import flash.utils.ByteArray;
import mx.utils.Base64Encoder;
/**
* US Secure Hash Algorithm 1 (SHA1)
*/
public class SHA1
{
private static var hexChars:String = "0123456789abcdef";
public static var digest:ByteArray;
/**
* Performs the SHA1 hash algorithm on a string.
*
* @param s The string to hash
* @return A string containing the hash value of s
* @langversion ActionScript 3.0
* @playerversion 9.0
* @tiptext
*/
public static function hash( s:String ):String
{
var blocks:Array = createBlocksFromString( s );
var byteArray:ByteArray = hashBlocks( blocks );
return toHex( byteArray.readInt(), true )
+ toHex( byteArray.readInt(), true )
+ toHex( byteArray.readInt(), true )
+ toHex( byteArray.readInt(), true )
+ toHex( byteArray.readInt(), true );
}
public static function hashBytes( data:ByteArray ):String
{
var blocks:Array = SHA1.createBlocksFromByteArray( data );
var byteArray:ByteArray = hashBlocks(blocks);
return toHex( byteArray.readInt(), true )
+ toHex( byteArray.readInt(), true )
+ toHex( byteArray.readInt(), true )
+ toHex( byteArray.readInt(), true )
+ toHex( byteArray.readInt(), true );
}
public static function hashToBase64( s:String ):String
{
var blocks:Array = SHA1.createBlocksFromString( s );
var byteArray:ByteArray = hashBlocks(blocks);
var charsInByteArray:String = "";
byteArray.position = 0;
for (var j:int = 0; j < byteArray.length; j++)
{
var byte:uint = byteArray.readUnsignedByte();
charsInByteArray += String.fromCharCode(byte);
}
var encoder:Base64Encoder = new Base64Encoder();
encoder.encode(charsInByteArray);
return encoder.flush();
}
private static function rol ( x:int, n:int ):int {
return ( x << n ) | ( x >>> ( 32 - n ) );
}
private static function ror ( x:int, n:int ):uint {
var nn:int = 32 - n;
return ( x << nn ) | ( x >>> ( 32 - nn ) );
}
private static function toHex( n:int, bigEndian:Boolean = false ):String {
var s:String = "";
if ( bigEndian ) {
for ( var i:int = 0; i < 4; i++ ) {
s += hexChars.charAt( ( n >> ( ( 3 - i ) * 8 + 4 ) ) & 0xF )
+ hexChars.charAt( ( n >> ( ( 3 - i ) * 8 ) ) & 0xF );
}
} else {
for ( var x:int = 0; x < 4; x++ ) {
s += hexChars.charAt( ( n >> ( x * 8 + 4 ) ) & 0xF )
+ hexChars.charAt( ( n >> ( x * 8 ) ) & 0xF );
}
}
return s;
}
private static function hashBlocks( blocks:Array ):ByteArray
{
var h0:int = 0x67452301;
var h1:int = 0xefcdab89;
var h2:int = 0x98badcfe;
var h3:int = 0x10325476;
var h4:int = 0xc3d2e1f0;
var len:int = blocks.length;
var w:Array = new Array( 80 );
for ( var i:int = 0; i < len; i += 16 ) {
var a:int = h0;
var b:int = h1;
var c:int = h2;
var d:int = h3;
var e:int = h4;
for ( var t:int = 0; t < 80; t++ ) {
if ( t < 16 ) {
// 6.1.a
w[ t ] = blocks[ i + t ];
} else {
// 6.1.b
w[ t ] = rol( w[ t - 3 ] ^ w[ t - 8 ] ^ w[ t - 14 ] ^ w[ t - 16 ], 1 );
}
// 6.1.d
var temp:int = rol( a, 5 ) + f( t, b, c, d ) + e + int( w[ t ] ) + k( t );
e = d;
d = c;
c = rol( b, 30 );
b = a;
a = temp;
}
// 6.1.e
h0 += a;
h1 += b;
h2 += c;
h3 += d;
h4 += e;
}
var byteArray:ByteArray = new ByteArray();
byteArray.writeInt(h0);
byteArray.writeInt(h1);
byteArray.writeInt(h2);
byteArray.writeInt(h3);
byteArray.writeInt(h4);
byteArray.position = 0;
digest = new ByteArray();
digest.writeBytes(byteArray);
digest.position = 0;
return byteArray;
}
private static function f( t:int, b:int, c:int, d:int ):int {
if ( t < 20 ) {
return ( b & c ) | ( ~b & d );
} else if ( t < 40 ) {
return b ^ c ^ d;
} else if ( t < 60 ) {
return ( b & c ) | ( b & d ) | ( c & d );
}
return b ^ c ^ d;
}
private static function k( t:int ):int {
if ( t < 20 ) {
return 0x5a827999;
} else if ( t < 40 ) {
return 0x6ed9eba1;
} else if ( t < 60 ) {
return 0x8f1bbcdc;
}
return 0xca62c1d6;
}
private static function createBlocksFromByteArray( data:ByteArray ):Array
{
var oldPosition:int = data.position;
data.position = 0;
var blocks:Array = new Array();
var len:int = data.length * 8;
var mask:int = 0xFF; // ignore hi byte of characters > 0xFF
for( var i:int = 0; i < len; i += 8 )
{
blocks[ i >> 5 ] |= ( data.readByte() & mask ) << ( 24 - i % 32 );
}
// append padding and length
blocks[ len >> 5 ] |= 0x80 << ( 24 - len % 32 );
blocks[ ( ( ( len + 64 ) >> 9 ) << 4 ) + 15 ] = len;
data.position = oldPosition;
return blocks;
}
private static function createBlocksFromString( s:String ):Array
{
var blocks:Array = new Array();
var len:int = s.length * 8;
var mask:int = 0xFF; // ignore hi byte of characters > 0xFF
for( var i:int = 0; i < len; i += 8 ) {
blocks[ i >> 5 ] |= ( s.charCodeAt( i / 8 ) & mask ) << ( 24 - i % 32 );
}
// append padding and length
blocks[ len >> 5 ] |= 0x80 << ( 24 - len % 32 );
blocks[ ( ( ( len + 64 ) >> 9 ) << 4 ) + 15 ] = len;
return blocks;
}
}
}
测试类:
public function init():void{
Alert.show(SHA1.hash("123456"));
}
Flex使用mx.utils.SHA256类来实现SHA256加密:
package
{
import flash.utils.ByteArray;
import mx.utils.SHA256;
public class SHA256_Test
{
public function SHA256_Test(){}
public static function sha_256(source : String):String{
var encryption : String;
var ba : ByteArray = new ByteArray();
ba.writeUTFBytes(source);
encryption = mx.utils.SHA256.computeDigest(ba);
return encryption;
}
}
}
测试类:
public function init():void{
Alert.show(SHA256_Test.sha_256("123456"));
}
分享到:
相关推荐
Delphi SHA1加密源码是用于在Delphi编程环境中实现SHA1散列算法的代码,它确保了在不同编程语言(如C#、PHP)中进行加密时的一致性。SHA1(Secure Hash Algorithm 1)是一种广泛使用的密码学安全散列函数,由美国...
标题中的“HMAC SHA1加密 C语言源码”是指一种基于哈希消息认证码(HMAC)和SHA1算法的加密方法,它在C语言环境中实现。HMAC(Hash-based Message Authentication Code)是一种用于验证数据完整性和来源的安全机制,...
这些源码涵盖了多种常见的对称和非对称加密算法,以及散列函数。以下是关于这些算法的详细介绍: 1. **3DES(Triple DES)**:3DES是一种对称加密算法,基于DES(Data Encryption Standard)进行改进。它通过三次...
MD5(Message-Digest Algorithm 5)、SHA1(Secure Hash Algorithm 1)和SHA256(Secure Hash Algorithm 2,256位版本)是常见的哈希函数,用于生成固定长度的数字指纹,常用于数据完整性校验、密码存储等领域。...
根据提供的文件信息,我们可以深入分析 SHA1(安全散列算法1)的相关实现细节与核心功能。SHA1 是一种被广泛使用的散列函数,用于生成一个固定长度(160位或20字节)的散列值。这些散列值通常用于数据完整性检查、...
易语言Base64_hmac_sha1加密算法源码,Base64_hmac_sha1加密算法,Base64_hmac_sha1,safe_add,bit_rol,sha1_kt,sha1_ft,rstr2b64,rstr2binb,binb_sha1,binb2rstr,rstr_hmac_sha1,逻辑右移
asp hmac_sha1加密算法完整源码_经测试无错版asp hmac_sha1加密算法完整源码_经测试无错版asp hmac_sha1加密算法完整源码_经测试无错版asp hmac_sha1加密算法完整源码_经测试无错版asp hmac_sha1加密算法完整源码_经...
总的来说,SHA1算法虽然在安全性上已不如后来的SHA256和SHA3等算法,但它的理解和实现对于学习密码学和信息安全基础仍然是非常有价值的。通过VS这样的集成开发环境,可以方便地学习和实践这种基础的哈希算法。
本项目提供了C语言实现的HMAC-SHA256和HMAC-SHA1加密算法,适用于VC2008开发环境。 首先,让我们深入理解HMAC-SHA256和HMAC-SHA1。HMAC是一种基于哈希函数的消息认证码,它结合了密钥和数据来生成一个固定长度的...
SHA256是一种广泛使用的密码散列函数,它能够生成一个256位(32字节)的散列值,常用于数据完整性校验和密码存储。在VB(Visual Basic)环境中,实现SHA256加密需要借助特定的库或者API。下面,我们将深入探讨SHA256...
学习和理解这个源代码,不仅可以帮助你深入理解SHA1算法的工作原理,还能提高你在密码学和信息安全方面的知识。 在实际应用中,SHA1虽然因为其安全性相比SHA256等更现代的哈希函数有所下降,但依然在某些场景下被...
综上所述,"SHA256 C源码.zip"包含的源代码可以帮助开发者理解和实现SHA256算法,这对于加密和网络安全相关的项目尤其有用。学习和研究这段代码能够加深对密码学原理的理解,同时也提供了在实际项目中应用该算法的...
易语言Base64_HMAC_SHA1加密算法源码是一个针对易语言编程环境设计的安全编码工具。这个压缩包包含了实现Base64编码与HMAC-SHA1哈希消息认证码(Hash-based Message Authentication Code)的源代码,对于学习和使用...
8. **SHA(Secure Hash Algorithm)**:SHA是一系列安全哈希算法,如SHA-1和SHA-256,它们生成固定长度的摘要,用于数据完整性验证。SHA家族的算法在密码学和数字证书中广泛应用。 在Delphi_d7加密算法大全中,你将...
asp hmac_sha1加密算法完整源码_本人用着的版本,微信分享必备
Vue-常用加密(MD5, DES, SHA256, BASE64, SHA1, RSA) Vue 中常见的加密算法可以分成三类:对称加密算法、非对称加密算法和 Hash 算法。下面将详细介绍每种加密算法的特点、优点和缺点,以及它们的应用场景。 一...
总之,SHA-1的LabVIEW实现是一个结合了密码学和图形化编程技术的实例,对于在LabVIEW环境中处理数据安全和验证问题的工程师来说,具有很高的实用价值。通过这样的实现,用户不仅可以对数据进行加密,还可以轻松地...
SHA1加密算法源代码 C语言,可直接运行
平时用的md5+sha1加密的js文件,用法: <script type="text/javascript" src="js/md5.js"></script> <script type="text/javascript" src="js/sha1.js"> var hash = hex_md5(name+" "+password); var date = new ...