Posted By

chrisaiv on 10/26/09


Tagged

SHA1 as3


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

Demian
TrentSkunk
zoo
donaldleegraham
tspitzr
jondiscipulo


AS3: SHa1 Encrypt


 / Published in: ActionScript 3
 

URL: http://www.erikhallander.com/blog/2008/lightweight-sha1-encryption-class-for-actionscript-3.html

I found this awesome class online and thought I should share it. It's a really nice, lightweight way to encrypt data out of ActionScript. Here's how to use it:

import com.utils.sha1Encrypt; var enc:sha1Encrypt = new sha1Encrypt(true); function sha1():void { trace(sha1Encrypt.encrypt("hello")); };

  1. /**
  2.  * com.utils.sha1encrypt.as
  3.  * aux lightweight function to return the SHA1 hex result of a since in ASCII or Unicode
  4.  * [email protected]
  5.  * NEVER SEND UNENCRYPTED DATA TO PHP FROM ACTIONSCRIPT!!
  6.  **/
  7. package com.utils {
  8. public class sha1Encrypt {
  9. private static var charInputBit:uint;
  10. /**
  11. * Class constructor
  12. * @param ASCII BOOLEAN True == ASCII False == UNICODE
  13. */
  14. public function sha1Encrypt(ASCII:Boolean) {
  15. ASCII ? charInputBit = 8 : charInputBit = 16;
  16. }
  17. /**
  18. * Public Method encrypt
  19. * @param s_source STRING
  20. * @returns STRING of HEX SHA1
  21. */
  22. public static function encrypt (s_source:String):String {
  23. return hex_sha1 (s_source);
  24. }
  25. private static function hex_sha1 (string:String):String {
  26. return bin_to_hex (sha1_convert( string_to_bin(string), string.length * charInputBit));
  27. }
  28. private static function sha1_convert (input:Array,amount:Number):Array {
  29.  
  30. var bit1:Number = 1732584193;
  31. var bit2:Number = -271733879;
  32. var bit3:Number = -1732584194;
  33. var bit4:Number = 271733878;
  34. var bit5:Number = -1009589776;
  35. var bitwise_op:Array = new Array();
  36.  
  37. input[amount >> 5] |= 0x80 << (24 - amount % 32);
  38. input[((amount + 64 >> 9) << 4) + 15] = amount;
  39.  
  40. for(var i:Number = 0; i < input.length; i += 16) {
  41. var stored_bit1:Number = bit1;
  42. var stored_bit2:Number = bit2;
  43. var stored_bit3:Number = bit3;
  44. var stored_bit4:Number = bit4;
  45. var stored_bit5:Number = bit5;
  46.  
  47. for(var j:Number = 0; j < 80; j++) {
  48. if(j < 16) bitwise_op[j] = input[i + j];
  49. else bitwise_op[j] = rol(bitwise_op[j-3] ^ bitwise_op[j-8] ^ bitwise_op[j-14] ^ bitwise_op[j-16], 1);
  50. var t:Number = safe_add (safe_add (rol (bit1, 5), sha_f_mod (j, bit2, bit3, bit4)), safe_add (safe_add (bit5, bitwise_op[j]), sha_z_mod (j)));
  51. bit5 = bit4;
  52. bit4 = bit3;
  53. bit3 = rol(bit2, 30);
  54. bit2 = bit1;
  55. bit1 = t;
  56. }
  57.  
  58. bit1 = safe_add(bit1, stored_bit1);
  59. bit2 = safe_add(bit2, stored_bit2);
  60. bit3 = safe_add(bit3, stored_bit3);
  61. bit4 = safe_add(bit4, stored_bit4);
  62. bit5 = safe_add(bit5, stored_bit5);
  63. }
  64. return [bit1, bit2, bit3, bit4, bit5];
  65. }
  66. private static function sha_f_mod (t:Number, b:Number, c:Number, d:Number):Number {
  67. if(t < 20) return (b & c) | ((~b) & d);
  68. if(t < 40) return b ^ c ^ d;
  69. if(t < 60) return (b & c) | (b & d) | (c & d);
  70. return b ^ c ^ d;
  71. }
  72. private static function sha_z_mod (t:Number):Number {
  73. return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : (t < 60) ? -1894007588 : -899497514;
  74. }
  75. private static function safe_add (x:Number, y:Number):Number {
  76. var lsw:Number = (x & 0xFFFF) + (y & 0xFFFF);
  77. var msw:Number = (x >> 16) + (y >> 16) + (lsw >> 16);
  78. return (msw << 16) | (lsw & 0xFFFF);
  79. }
  80. private static function rol (num:Number, cnt:Number):Number {
  81. return (num << cnt) | (num >>> (32 - cnt));
  82. }
  83. private static function string_to_bin (str:String):Array {
  84. var bin:Array = new Array ();
  85. var mask:Number = (1 << charInputBit) - 1;
  86. for (var i:Number = 0; i < str.length * charInputBit; i += charInputBit) bin[i>>5] |= (str.charCodeAt (i / charInputBit) & mask) << (32 - charInputBit - i%32);
  87. return bin;
  88. }
  89. private static function bin_to_hex (binarray:Array):String {
  90. var charMap:String = "0123456789abcdef";
  91. var str:String = new String();
  92. for(var i:Number = 0; i < binarray.length * 4; i++) {
  93. str += charMap.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
  94. charMap.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF);
  95. }
  96. return str;
  97. }
  98. }
  99. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: the_coder on April 28, 2011

This code does NOT WORK properly. It gives a different "SHA1" hash than any other script, so it's probably wrong.

Posted By: chrisaiv on May 4, 2011

Thanks for fact checking!

You need to login to post a comment.