title: linux ascii to utf-16 (then sha1 and base64) encode
body: <p>We have a communication protocol that requires us to Base64 Encoded a SHA1 hash of a UTF-16 encoded password.  We have been given Java, javascript, and visual basic examples however we are running under Linux (redhat)</p>
  
  <p>the provided test string: <code>TESTED@8691</code><br>
  the final output: <code>rBbBKqbJodT5awZal/CSCYF/sFo=</code></p>
  
  <p>I have tried</p>
  
  <pre><code>iconv_t conv = iconv_open("UTF-16LE","ASCII"); // open succeeds
  char *from_string=strdup("TESTED@8691");
  size_t from_length=strlen(from_string);
  size_t to_length=from_length*3;
  size_t original_to_length=to_length;
  
  char *to_string=(char*)calloc(1,to_length);
  int convert_return=iconv(conv,&amp;from_string,&amp;from_length,&amp;to_string,&amp;to_length);
  // convert_return is 0 indicating success, to_length is 11, from_length is 0
  </code></pre>
  
  <p>run sha1 and base64 encoding on to_string with a length of 22<br>
  resulting output: <code>GCXe7HMDoq/NRqo1WWYJDDYZzP0=</code></p>
  
  <p>If I loop through to_string I get:</p>
  
  <pre><code>for (int i=0; i&lt;original_to_length-to_length; ++i) {
     printf("to_string %d = %x",i,to_string[i]);  
  }
  
  output:
  to_string 0 = 0
  to_string 1 = 0
  to_string 2 = 0
  to_string 3 = 0
  to_string 4 = 0
  to_string 5 = 0
  to_string 6 = 0
  to_string 7 = 0
  to_string 8 = 0
  to_string 9 = 0
  to_string 10 = 0
  to_string 11 = 0
  to_string 12 = 0
  to_string 13 = 0
  to_string 14 = 21
  to_string 15 = 0
  to_string 16 = 0
  to_string 17 = 0
  to_string 18 = 4
  to_string 19 = 7e
  to_string 20 = 13
  to_string 21 = e
  </code></pre>
  
  <p>Here is the javascript conversion:</p>
  
  <pre><code>function str2rstr_utf16le(input)
  {
     var output = "";
     for(var i = 0; i &lt; input.length; i++)
      output += String.fromCharCode( input.charCodeAt(i) &amp; 0xFF,
                                (input.charCodeAt(i) &gt;&gt;&gt; 8) &amp; 0xFF);
  
     return output;
  }
  </code></pre>
  
  <p>What am I missing?<br>
  Thank You</p>