AS3: Unique ID's for Users


/ Published in: ActionScript 3
Save to your folder(s)

Awesome piece of code that is executed by "GUID.create()". Check out this website. http://www.rgbeffects.com


Copy this code and paste it in your HTML
  1. package {
  2.  
  3. import flash.display.Sprite;
  4. import flash.system.Capabilities;
  5.  
  6. public class GUID extends Sprite {
  7.  
  8. private static var counter:Number = 0;
  9.  
  10. function GUID(){
  11.  
  12. }
  13.  
  14. public static function create():String {
  15. var dt:Date = new Date();
  16. var id1:Number = dt.getTime();
  17. var id2:Number = Math.random()*Number.MAX_VALUE;
  18. var id3:String = Capabilities.serverString;
  19. var rawID:String = calculate(id1+id3+id2+counter++).toUpperCase();
  20. var finalString:String = rawID.substring(0, 8) + "-" + rawID.substring(8, 12) + "-" + rawID.substring(12, 16) + "-" + rawID.substring(16, 20) + "-" + rawID.substring(20, 32);
  21. return finalString;
  22. }
  23.  
  24. private static function calculate(src:String):String {
  25. return hex_sha1(src);
  26. }
  27.  
  28. private static function hex_sha1(src:String):String {
  29. return binb2hex(core_sha1(str2binb(src), src.length*8));
  30. }
  31.  
  32. private static function core_sha1(x:Array, len:Number):Array {
  33. x[len >> 5] |= 0x80 << (24-len%32);
  34. x[((len+64 >> 9) << 4)+15] = len;
  35. var w:Array = new Array(80), a:Number = 1732584193;
  36. var b:Number = -271733879, c:Number = -1732584194;
  37. var d:Number = 271733878, e:Number = -1009589776;
  38. for (var i:Number = 0; i<x.length; i += 16) {
  39. var olda:Number = a, oldb:Number = b;
  40. var oldc:Number = c, oldd:Number = d, olde:Number = e;
  41. for (var j:Number = 0; j<80; j++) {
  42. if (j<16) w[j] = x[i+j];
  43. else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
  44. var t:Number = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), safe_add(safe_add(e, w[j]), sha1_kt(j)));
  45. e = d; d = c;
  46. c = rol(b, 30);
  47. b = a; a = t;
  48. }
  49. a = safe_add(a, olda);
  50. b = safe_add(b, oldb);
  51. c = safe_add(c, oldc);
  52. d = safe_add(d, oldd);
  53. e = safe_add(e, olde);
  54. }
  55. return new Array(a, b, c, d, e);
  56. }
  57.  
  58. private static function sha1_ft(t:Number, b:Number, c:Number, d:Number):Number {
  59. if (t<20) return (b & c) | ((~b) & d);
  60. if (t<40) return b ^ c ^ d;
  61. if (t<60) return (b & c) | (b & d) | (c & d);
  62. return b ^ c ^ d;
  63. }
  64.  
  65. private static function sha1_kt(t:Number):Number {
  66. return (t<20) ? 1518500249 : (t<40) ? 1859775393 : (t<60) ? -1894007588 : -899497514;
  67. }
  68.  
  69. private static function safe_add(x:Number, y:Number):Number {
  70. var lsw:Number = (x & 0xFFFF)+(y & 0xFFFF);
  71. var msw:Number = (x >> 16)+(y >> 16)+(lsw >> 16);
  72. return (msw << 16) | (lsw & 0xFFFF);
  73. }
  74.  
  75. private static function rol(num:Number, cnt:Number):Number {
  76. return (num << cnt) | (num >>> (32-cnt));
  77. }
  78.  
  79. private static function str2binb(str:String):Array {
  80. var bin:Array = new Array();
  81. var mask:Number = (1 << 8)-1;
  82. for (var i:Number = 0; i<str.length*8; i += 8) {
  83. bin[i >> 5] |= (str.charCodeAt(i/8) & mask) << (24-i%32);
  84. }
  85. return bin;
  86. }
  87.  
  88. private static function binb2hex(binarray:Array):String {
  89. var str:String = new String("");
  90. var tab:String = new String("0123456789abcdef");
  91. for (var i:Number = 0; i<binarray.length*4; i++) {
  92. str += tab.charAt((binarray[i >> 2] >> ((3-i%4)*8+4)) & 0xF) + tab.charAt((binarray[i >> 2] >> ((3-i%4)*8)) & 0xF);
  93. }
  94. return str;
  95. }
  96. }
  97. }

URL: http://www.rgbeffects.com/blog/development/actionscript-3-guid-generating-unique-ids-for-users-in-as3/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.