<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" > <head><meta content="text/html;charset=&quot;utf-8&quot;" http-equiv="Content-type"/><link href="faldoc.css" rel="stylesheet" type="text/css"/><title> - Class HashBase</title></head><body class="faldoc"><ul class="navi_top"><li class="top"><a href="index.html">Top: Table of contents</a></li> <li class="up"><a href="feathers_hash.html">Up: Hashing algorighms</a></li> <li class="prev"><a href="feathers_hash_weak_hashes.html">Previous: Weak hashes</a></li> <li class="next"><a href="feathers_hash_function.html">Next: Functions</a></li> <li class="clear"></li> </ul><div id="page_body"><h1><span class="toc_number">2.5.4</span>Class HashBase</h1><p class="brief">Base class for each hash algorithm, specialized for overloading. </p> <pre class="prototype">Class HashBase</pre> <p>The HashBase class provides a set of shared interfaces that are syntactically equivalent for each specialized hash. </p> <p>Hashes are generated by creating an instance of a specialized class and putting data into it. When the result is requested, a hash is finalized, which means that no more data can be added; any attempts to do so will raise an exception. </p> <p>Basic usage example: </p> <pre> crc = CRC32() crc.update("abc") > crc // prints "352441c2" </pre><p class='note'><b>Note:</b> Instantiating HashBase directly and calling any method will raise an error. </p> <h2><a name="hashbase_overload">Overloading HashBase</a></h2> <p>To easily implement other hash algorithms in native falcon code, HashBase can be overloaded. For simplicity, only 2 methods have to be overloaded, and 2 new methods have to be added: </p> <pre> class MyHash from HashBase state = nil // internal state outp = nil function bytes(): return 12 // must be overloaded and return a constant integer > 0 function toMemBuf(): return self.outp // must be overloaded and return a MemBuf with wordSize 1 and length equal to bytes() function process(buf) // must be declared, as it is invoked by the module on update() calls // *mangle MemBuf and update state* end function finalize() // must be declared, as it is invoked by the module to produce the actual digest // *transform state and assign result MemBuf(1, bytes()) to outp* end end </pre><p>How this works: <ul><li><b>bytes()</b> is internally invoked by bits() (once, the returned integer is cached by the module) </li><li><b>process()</b> is invoked by update() and updateInt(), beeing passed a MemBuf with word size 1 </li><li><b>toMemBuf()</b> is invoked by toString() and toInt() </li><li><b>finalize()</b> is called ONCE before toMemBuf() and is intended to do process remaining buffers, and produce the actual digest. Does not have to be called manually. </li></ul></p> <p class='note'><b>Note:</b> You are strongly advised NOT to overload any other methods except the four above, unless you REALLY know what you're doing. </p> <p>Advantages of doing it this way: <ul><li>It is not necessary to implement update() in native falcon code. </li><li>All value endian conversions, type mangling, and error checking is done by the module, so focus can be set on the algorithm itself. </li><li>The values returned by bytes(), toMemBuf() and toInt() are cached by the module, means less calls, less time. </li><li>The module ensures that finalize() is called only once, no explicit checking required. </li></ul></p> </div><ul class="navi_bottom"><li class="top"><a href="index.html">Top: Table of contents</a></li> <li class="up"><a href="feathers_hash.html">Up: Hashing algorighms</a></li> <li class="prev"><a href="feathers_hash_weak_hashes.html">Previous: Weak hashes</a></li> <li class="next"><a href="feathers_hash_function.html">Next: Functions</a></li> <li class="clear"></li> </ul><div class="signature">Made with <a href="faldoc 3.0">http://www.falconpl.org</a></div></body></html>