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

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.