/js/lib/Socket.IO-node/support/socket.io-client/lib/vendor/web-socket-js/flash-src/com/hurlant/util/der/ObjectIdentifier.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs · ActionScript · 112 lines · 91 code · 10 blank · 11 comment · 17 complexity · 41271312d681fa1c7285bc8947519e44 MD5 · raw file

  1. /**
  2. * ObjectIdentifier
  3. *
  4. * An ASN1 type for an ObjectIdentifier
  5. * We store the oid in an Array.
  6. * Copyright (c) 2007 Henri Torgemane
  7. *
  8. * See LICENSE.txt for full license information.
  9. */
  10. package com.hurlant.util.der
  11. {
  12. import flash.utils.ByteArray;
  13. public class ObjectIdentifier implements IAsn1Type
  14. {
  15. private var type:uint;
  16. private var len:uint;
  17. private var oid:Array;
  18. public function ObjectIdentifier(type:uint, length:uint, b:*) {
  19. this.type = type;
  20. this.len = length;
  21. if (b is ByteArray) {
  22. parse(b as ByteArray);
  23. } else if (b is String) {
  24. generate(b as String);
  25. } else {
  26. throw new Error("Invalid call to new ObjectIdentifier");
  27. }
  28. }
  29. private function generate(s:String):void {
  30. oid = s.split(".");
  31. }
  32. private function parse(b:ByteArray):void {
  33. // parse stuff
  34. // first byte = 40*value1 + value2
  35. var o:uint = b.readUnsignedByte();
  36. var a:Array = []
  37. a.push(uint(o/40));
  38. a.push(uint(o%40));
  39. var v:uint = 0;
  40. while (b.bytesAvailable>0) {
  41. o = b.readUnsignedByte();
  42. var last:Boolean = (o&0x80)==0;
  43. o &= 0x7f;
  44. v = v*128 + o;
  45. if (last) {
  46. a.push(v);
  47. v = 0;
  48. }
  49. }
  50. oid = a;
  51. }
  52. public function getLength():uint
  53. {
  54. return len;
  55. }
  56. public function getType():uint
  57. {
  58. return type;
  59. }
  60. public function toDER():ByteArray {
  61. var tmp:Array = [];
  62. tmp[0] = oid[0]*40 + oid[1];
  63. for (var i:int=2;i<oid.length;i++) {
  64. var v:int = parseInt(oid[i]);
  65. if (v<128) {
  66. tmp.push(v);
  67. } else if (v<128*128) {
  68. tmp.push( (v>>7)|0x80 );
  69. tmp.push( v&0x7f );
  70. } else if (v<128*128*128) {
  71. tmp.push( (v>>14)|0x80 );
  72. tmp.push( (v>>7)&0x7f | 0x80 );
  73. tmp.push( v&0x7f);
  74. } else if (v<128*128*128*128) {
  75. tmp.push( (v>>21)|0x80 );
  76. tmp.push( (v>>14) & 0x7f | 0x80 );
  77. tmp.push( (v>>7) & 0x7f | 0x80 );
  78. tmp.push( v & 0x7f );
  79. } else {
  80. throw new Error("OID element bigger than we thought. :(");
  81. }
  82. }
  83. len = tmp.length;
  84. if (type==0) {
  85. type = 6;
  86. }
  87. tmp.unshift(len); // assume length is small enough to fit here.
  88. tmp.unshift(type);
  89. var b:ByteArray = new ByteArray;
  90. for (i=0;i<tmp.length;i++) {
  91. b[i] = tmp[i];
  92. }
  93. return b;
  94. }
  95. public function toString():String {
  96. return DER.indent+oid.join(".");
  97. }
  98. public function dump():String {
  99. return "OID["+type+"]["+len+"]["+toString()+"]";
  100. }
  101. }
  102. }