?
Solved

linux ascii to utf-16 (then sha1 and base64) encode

Posted on 2012-03-13
4
Medium Priority
?
1,011 Views
Last Modified: 2012-03-18
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) the provided test string:TESTED@8691 the final output:rBbBKqbJodT5awZal/CSCYF/sFo= I have tried

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,&from_string,&from_length,&to_string,&to_length);
// convert_return is 0 indicating success, to_length is 22, from_length is 0

Open in new window


run sha1 and base64 encoding on to_string with a length of 22 resulting output: GCXe7HMDoq/NRqo1WWYJDDYZzP0=

If I loop through to_string I get:

for (int i=0; i<original_to_length-to_length; ++i) {
   printf("to_string %d = %x",i,to_string[i]);  
}

Open in new window


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
What am I missing? Thank You
0
Comment
Question by:PhilC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 35

Expert Comment

by:sarabande
ID: 37716160
utf-16 means the input string must be wchar_t* (2 bytes per character, typedef of short int) .

you were using char* for input what cannot work in my opinion.

Sara
0
 

Author Comment

by:PhilC
ID: 37716441
iconv() accepts a char* that is why I used it.  changing to wchar_t* still does not work
Thank you
0
 

Accepted Solution

by:
PhilC earned 0 total points
ID: 37716570
I have found the answer, I read but did not properly consider the man page, "The  iconv  function converts one multibyte character at a
time, and for  each  character  conversion  it  increments
*inbuf  and  decrements *inbytesleft by the number of con­
verted input bytes, it increments *outbuf  and  decrements
*outbytesleft by the number of converted output bytes, and
it updates the conversion state contained in cd."
so my *to_string needed to be put back to the beginning
thank you
0
 

Author Closing Comment

by:PhilC
ID: 37734393
found the solution
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question