0

I need to undergo a process of creating a signature from the following process in Objective C:

  1. ASCII encode an NSString
  2. Hash the results with MD5
  3. Perform bitwise operation on the resulting bytes (&127)
  4. Convert to Base64 string

I am stuck on where to start. I am able to complete this task in C# but am at a loss in Objective C or even ANSI C.

I have got as far as getting the UTF8String from the initial NSString using:

[NSString UTF8String]

Hopefully you can help.

JordanMazurke
  • 1,103
  • 10
  • 22
  • Why are you using ASCII? It is not a lossless conversion. Tackle each of these steps individually, each one is easy to search for. – Paul de Lange Aug 14 '12 at 12:58
  • It is not my definition - it is a set of instructions supplied by a third party that I have to follow. I can handle points 2 and 4 but 1 and 3, in particular, are causing me issues – JordanMazurke Aug 14 '12 at 13:18

1 Answers1

1
  1. Read String Conversions
  2. Dowload NSString MD5 Category by H2CO3
  3. Take your pick of Base64 encoders

You mentioned you are having problems with String conversions, this is what you want to convert to ASCII:

NSString* src = @"";
NSData* data = [src dataUsingEncoding: NSASCIIStringEncoding allowLossyConversion: YES];
NSString* ascii = [[NSString alloc] initWithData: data encoding: NSASCIIStringEncoding];

To perform the bitwise operations is a little complicated. You will want to do this in straight C, like so:

NSString* src = <MD5 hashed result>
NSMutableString* dst = [[NSMutableString alloc] initWithCapacity: src.length];
for(NSUInteger i=0;i<src.length;i++) {
    unichar c = [src characterAtIndex: i];
    [dst appendFormat: @"%d", (c & 127)];
}

//Base64 encode dst

The 3rd party should tell you how to do it if they require it.

Community
  • 1
  • 1
Paul de Lange
  • 10,613
  • 10
  • 41
  • 56
  • 1
    You are very right - they should have examples if they want compliance. Thank you very much for taking the time to help. I will try it out now. – JordanMazurke Aug 14 '12 at 14:00