1 19 20 package com.sslexplorer.security.pki; 21 22 import java.io.ByteArrayOutputStream ; 23 24 25 30 public class SimpleASNWriter { 31 private ByteArrayOutputStream data; 32 33 36 public SimpleASNWriter() { 37 this.data = new ByteArrayOutputStream (); 38 } 39 40 45 public void writeByte(int b) { 46 data.write(b); 47 } 48 49 54 public void writeData(byte[] b) { 55 writeLength(b.length); 56 this.data.write(b, 0, b.length); 57 } 58 59 64 public void writeLength(int length) { 65 if (length < 0x80) { 66 data.write(length); 67 } else { 68 if (length < 0x100) { 69 data.write(0x81); 70 data.write(length); 71 } else if (length < 0x10000) { 72 data.write(0x82); 73 data.write(length >>> 8); 74 data.write(length); 75 } else if (length < 0x1000000) { 76 data.write(0x83); 77 data.write(length >>> 16); 78 data.write(length >>> 8); 79 data.write(length); 80 } else { 81 data.write(0x84); 82 data.write(length >>> 24); 83 data.write(length >>> 16); 84 data.write(length >>> 8); 85 data.write(length); 86 } 87 } 88 } 89 90 95 public byte[] toByteArray() { 96 return data.toByteArray(); 97 } 98 } 99 | Popular Tags |