public string traceToStr() { Buf buf = new MemBuf(1024); trace(buf.@out()); return(buf.flip().readAllStr()); }
public static Buf toBuf(string self, Charset charset) { MemBuf buf = new MemBuf(self.Length * 2); buf.charset(charset); buf.print(self); return(buf.flip()); }
public Buf dup() { int size = (int)this.size(); byte[] copy = new byte[size]; getBytes(0, copy, 0, size); Buf result = new MemBuf(copy, size); result.charset(charset()); return(result); }
/** * Make a thread-safe copy of the specified object. * If it is immutable, then just return it; otherwise * we make a serialized copy. */ public static object safe(object obj) { if (obj == null) { return(null); } if (FanObj.isImmutable(obj)) { return(obj); } Buf buf = new MemBuf(512); buf.writeObj(obj); buf.flip(); return(buf.readObj()); }
public Buf getRange(Range range) { long size = getSize(); long s = range.start(size); long e = range.end(size); int n = (int)(e - s + 1); if (n < 0) { throw IndexErr.make(range).val; } byte[] slice = new byte[n]; getBytes(s, slice, 0, n); Buf result = new MemBuf(slice, n); result.charset(charset()); return(result); }
public Buf getRange(Range range) { long size = getSize(); long s = range.start(size); long e = range.end(size); int n = (int)(e - s + 1); if (n < 0) throw IndexErr.make(range).val; byte[] slice = new byte[n]; getBytes(s, slice, 0, n); Buf result = new MemBuf(slice, n); result.charset(charset()); return result; }
public Buf dup() { int size = (int)this.size(); byte[] copy = new byte[size]; getBytes(0, copy, 0, size); Buf result = new MemBuf(copy, size); result.charset(charset()); return result; }
internal MemBufInStream(MemBuf parent) { this.p = parent; }
public override Buf hmac(string algorithm, Buf keyBuf) { // get digest algorthim string alg = algorithm; if (alg == "SHA-1") { alg = "SHA1"; // to make .NET happy } HashAlgorithm ha = HashAlgorithm.Create(alg); if (ha == null) { throw ArgErr.make("Unknown digest algorthm: " + algorithm).val; } // get secret key bytes int blockSize = 64; byte[] keyBytes = null; int keySize = 0; try { // get key bytes MemBuf keyMemBuf = (MemBuf)keyBuf; keyBytes = keyMemBuf.m_buf; keySize = keyMemBuf.m_size; // key is greater than block size we hash it first if (keySize > blockSize) { keyBytes = ha.ComputeHash(keyBytes, 0, keySize); keySize = keyBytes.Length; } } catch (System.InvalidCastException) { throw UnsupportedErr.make("key parameter must be memory buffer").val; } // RFC 2104: // ipad = the byte 0x36 repeated B times // opad = the byte 0x5C repeated B times // H(K XOR opad, H(K XOR ipad, text)) MemBuf acc = new MemBuf(1024); // inner digest: H(K XOR ipad, text) for (int i = 0; i < blockSize; ++i) { if (i < keySize) { acc.write((byte)(keyBytes[i] ^ 0x36)); } else { acc.write((byte)0x36); } } acc.pipeFrom(m_buf, 0, m_size); byte[] innerDigest = ha.ComputeHash(acc.m_buf, 0, acc.m_size); // outer digest: H(K XOR opad, innerDigest) acc.clear(); for (int i = 0; i < blockSize; ++i) { if (i < keySize) { acc.write((byte)(keyBytes[i] ^ 0x5C)); } else { acc.write((byte)0x5C); } } acc.pipeFrom(innerDigest, 0, innerDigest.Length); // return result return(new MemBuf(ha.ComputeHash(acc.m_buf, 0, acc.m_size))); }
public static Buf toBuf(string self, Charset charset) { MemBuf buf = new MemBuf(self.Length*2); buf.charset(charset); buf.print(self); return buf.flip(); }
public string traceToStr() { Buf buf = new MemBuf(1024); trace(buf.@out()); return buf.flip().readAllStr(); }
/** * Make a thread-safe copy of the specified object. * If it is immutable, then just return it; otherwise * we make a serialized copy. */ public static object safe(object obj) { if (obj == null) return null; if (FanObj.isImmutable(obj)) return obj; Buf buf = new MemBuf(512); buf.writeObj(obj); buf.flip(); return buf.readObj(); }
internal MemBufOutStream(MemBuf parent) { this.p = parent; }
public override Buf hmac(string algorithm, Buf keyBuf) { // get digest algorthim string alg = algorithm; if (alg == "SHA-1") alg = "SHA1"; // to make .NET happy HashAlgorithm ha = HashAlgorithm.Create(alg); if (ha == null) throw ArgErr.make("Unknown digest algorthm: " + algorithm).val; // get secret key bytes int blockSize = 64; byte[] keyBytes = null; int keySize = 0; try { // get key bytes MemBuf keyMemBuf = (MemBuf)keyBuf; keyBytes = keyMemBuf.m_buf; keySize = keyMemBuf.m_size; // key is greater than block size we hash it first if (keySize > blockSize) { keyBytes = ha.ComputeHash(keyBytes, 0, keySize); keySize = keyBytes.Length; } } catch (System.InvalidCastException) { throw UnsupportedErr.make("key parameter must be memory buffer").val; } // RFC 2104: // ipad = the byte 0x36 repeated B times // opad = the byte 0x5C repeated B times // H(K XOR opad, H(K XOR ipad, text)) MemBuf acc = new MemBuf(1024); // inner digest: H(K XOR ipad, text) for (int i=0; i<blockSize; ++i) { if (i < keySize) acc.write((byte)(keyBytes[i] ^ 0x36)); else acc.write((byte)0x36); } acc.pipeFrom(m_buf, 0, m_size); byte[] innerDigest = ha.ComputeHash(acc.m_buf, 0, acc.m_size); // outer digest: H(K XOR opad, innerDigest) acc.clear(); for (int i=0; i<blockSize; ++i) { if (i < keySize) acc.write((byte)(keyBytes[i] ^ 0x5C)); else acc.write((byte)0x5C); } acc.pipeFrom(innerDigest, 0, innerDigest.Length); // return result return new MemBuf(ha.ComputeHash(acc.m_buf, 0, acc.m_size)); }