title: Node crypto aes256-cbc 0x0 padding example
body: <p>From node cryptos <a href="http://nodejs.org/api/crypto.html#crypto_cipher_setautopadding_auto_padding_true" rel="nofollow">documentation</a>:</p>
  
  <blockquote>
    <p>"You can disable automatic padding of the input data to block size. If auto_padding is false, the length of the entire input data must be a multiple of the cipher's block size or final will fail. Useful for non-standard padding, e.g. using 0x0 instead of PKCS padding. You must call this before cipher.final." </p>
  </blockquote>
  
  <p>Which is all nice, however, i wasn't able to find a proper example anywhere:</p>
  
  <p>Here are my thoughts on the manner:</p>
  
  <pre><code>function pad(str) {
    if((str.length*8)%256 != 0) {
      str+= "0";
      pad(str);
    } 
  }
  
  var str = "blah_blah_blah_blah_ asdf";
  if(byteLength &lt; 256) {
    for(i=byteLength;i&lt;256;i+=8) {
      str += "0";
    }
  } else if(byteLength &gt; 256) {
    pad(str);
  }
  </code></pre>
  
  <p>Now this is obviously not ideal since the cipher.final() method should remove the padding,
  however it won't, also, i should probably pad it with hexes rather than chars.</p>
  
  <p>Also, the modulus function fails on strings greater than 256 <em>shrug</em></p>
  
  <p>What's the proper way to use custom padding(0x0) with node crypto?</p>