If I need to obfuscate an iPhone password that is hardcode (Oauth Client Identifier and Client Secret), would this be a way to do it?
NSString *a = #"a";
NSString *b = #"b";
NSString *c = #"c";
NSString *d = #"d";
NSString *e = #"e";
NSString *f = #"f";
NSString *g = #"g";
NSString *h = #"h";
NSString *i = #"i";
/* hidden */
NSString *w = #"w";
NSString *x = #"x";
NSString *y = #"y";
NSString *z = #"z";
NSString *pwd = [NSString stringWithFormat:#"%#%#%#%#%#%#%#%#", p,a,s,s,w,o,r,d];
I know obfuscate isn't recommended but after reading this OAuth secrets in mobile apps it seems like the only way.
If you compile with clang -O3, only the letters that are actually used in the password get included in the .o file. You should include some code that pretends to use the rest of the alphabet, such as another call to + stringWithFormat whose results are ignored.
You could always encrypt the text in question offline, store the encrypted version in the app, then at the point where you need it, decrypt it. That way it (at least) isn't in plain text in the app. Even your mechanism above will likely produce a pattern in the binary.
Related
I know HMAC-SHA1 functions are easily available on SO, but I have tried all of them to generate an OAuth signature but with no success.
Since the code sample given by yahoo
is written in Java I am not sure If I am following the same HMAC-SHA1 algorithm.
Here is the method which I use to generate it:
- (NSString *)generateOAuthHeader
{
NSString *apiURL = #"https://weather-ydn-yql.media.yahoo.com/forecastrss";
NSString *oauth_consumer_key = #"dj0yJmk9V004dENIbkd6dXh3JnM9Y29uc3VtZXJzZWNyZXQmc3Y9MCZ4PTg0";
NSString *consumerSecret = #"9b54fad8d2bccedaa17eddfe342a0178ee72eb34";
NSString *oauth_nonce = #"840eee23-f521-4d52-bca9-3a715894f22";
NSString *oauth_signature_method = #"HMAC-SHA1";
NSString *oauth_timestamp = [NSString stringWithFormat:#"%.0f", [[NSDate date] timeIntervalSince1970]];
NSString *oauth_version = #"1.0";
NSString *encodedApiURL = urlformdata_encode(apiURL);
NSString *parameters = NSString *parameters = [NSString stringWithFormat:#"oauth_consumer_key=%#&oauth_nonce=%#&oauth_signature_method=%#&oauth_timestamp=%#&oauth_version=%#&lat=%f&lon=%f&format=json", oauth_consumer_key, oauth_nonce, oauth_signature_method, oauth_timestamp, oauth_version, 30.707640, 76.703553, nil];
NSString *encodedParameters = urlformdata_encode(parameters);
NSString *signature = [NSString stringWithFormat:#"GET&%#&%#&", encodedApiURL, encodedParameters];
signature = [self hmacsha1:signature secret:consumerSecret];
NSString *authorizationHeader = [NSString stringWithFormat:#"OAuth oauth_consumer_key=\"%#\", oauth_nonce=\"%#\", oauth_signature_method=\"%#\", oauth_timestamp=\"%#\", oauth_version=\"%#\", oauth_signature=\"%#\"", oauth_consumer_key, oauth_nonce, oauth_signature_method, oauth_timestamp, oauth_version, signature, nil];
return authorizationHeader;
}
But I always end up having a 401 error meaning the signature is not correct.
I created a public repo in objective-c so any one can try it out, it is available here: https://github.com/userException/yahooOAuthiOS
The one minute detail which is not mentioned on Yahoo's page is you have to append "&" to the consumer secret while creating HMA-SHA1 encrypted string. Because of this the HMAC-SHA1 string was not what the yahoo's server was comparing it with.
I have committed my changes in the same repo mentioned in the question, if you need to have the Swift/Objective-C version of it.
My intention is to read all the system log messages pertaining to my application(all those log messages present inside the system.log file), and store it in a log file within the app's Document folder.
I went through this code and used this answer.
Here is my code:
NSDate *currentDate = [[NSDate alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:[NSString stringWithFormat: #"dd-MM-yyyy-HHmmss"]];
NSString *logFileName = [[formatter stringFromDate:currentDate] stringByAppendingString:#".log"];
NSError *error;
NSMutableString *content = [[NSMutableString alloc] init];
aslmsg q, m;
int i;
const char *key, *val;
q = asl_new(ASL_TYPE_QUERY);
aslresponse r = asl_search(NULL, q);
while (NULL != (m = asl_next(r)))
{
NSMutableDictionary *tmpDict = [NSMutableDictionary dictionary];
for (i = 0; (NULL != (key = asl_key(m, i))); i++)
{
NSString *keyString = [NSString stringWithUTF8String:(char *)key];
val = asl_get(m, key);
NSString *string = [NSString stringWithUTF8String:val];
[tmpDict setObject:string forKey:keyString];
}
[content appendString:[NSString stringWithFormat:#"%#",tmpDict]];
}
asl_release(r);
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:logFileName];
[[NSString stringWithString:content] writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
This is just a part of code of my IOS app. I am running my app on the IOS Simulator.
The log files gets successfully created. It has some content which is predominantly JSON.
However, the file does not have those log messages which I outputting through NSLog, yet those log messages are present inside the System's log file.
I am quite new to IOS Development. As stated, my purpose to access the System's log files. So I want to capture each log message and save it into my personal directory.
I searched a lot on this Stackoverflow. However, I haven't found the exact thread which could help me on this. Can anyone guide me appropriately?
(I hope this question isn't duplicated.)
I guess you want them to check them later in case you find an error. Maybe you can try Bugfender (http://bugfender.com), it's a tool we have created to get the logs from our customers while developing apps.
With it, you are going to be able to get also all NSLog calls.
The answers you have checked are the right way to do it if you don't want to use Bugfender. In our case we are using the functions documented here
In my app, I have a view where user have to fill a form. But, sometime the app crash here, in this function, that simple cacth the value field and built a url to give
-(NSString*)urlToUpload{
NSString *string1 =[[NSString alloc]init];
string1= [NSString stringWithFormat:#"?nombre="];
NSString *string2 = [string1 stringByAppendingString:nameAdded];
//crash here
NSString *string3 = [string2 stringByAppendingString:#"&horario="];
NSString *string4 = [string3 stringByAppendingString:horarioAdded];
NSString *string5 = [string4 stringByAppendingString:#"&info="];
NSString *string6 = [string5 stringByAppendingString:infoAdded];
NSString *string7 = [string6 stringByAppendingString:#"&offerta="];
NSString *string8 = [string7 stringByAppendingString:offertaAdded];
NSString *lat_string = [[[NSString alloc] initWithFormat:#"%f",locationToUpload2.latitude] autorelease];
NSString *lon_string = [[[NSString alloc] initWithFormat:#"%f",locationToUpload2.longitude] autorelease];
NSString *string9 = [string8 stringByAppendingString:#"&latitude="];
NSString *string10 = [string9 stringByAppendingString:lat_string];
NSString *string11 = [string10 stringByAppendingString:#"&longitude="];
NSString *string12 = [string11 stringByAppendingString:lon_string];
NSString *url1 = [NSString stringWithFormat:#"http://myserverside/mysql_up.php"];
NSString *url = [url1 stringByAppendingString:string12];
return url;
}
EDIT:
It seems problem appers on nameAdded when there is a white space into textField(i.e. MisterB not crash, Mister B yes ).
But I am using:
nameAdded =[[nameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
and NSLOg give of nameAdded is Mister%20B.
The crash still appearing...
Just use a single stringWithFormat::
- (NSString *)urlToUpload {
NSString *url = [NSString stringWithFormat:#"http://myserverside/mysql_up.php?nombre=%#&horario=%#&info=%#&offerta=%#&latitude=%f&longitude=%f",
[nameAdded stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[horarioAdded stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[infoAdded stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[offertaAdded stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
locationToUpload2.latitude, locationToUpload2.longitude];
return url;
}
Make sure the referenced variables are valid.
In your original code there is no need to alloc/init an NSString then assign another string to the same variable. That's a memory leak (string1).
If you really want to structure your code the way you have it, at least use an NSMutableString and append to that one mutable string. Creating over a dozen NSString variables is the wrong way to do it.
Updated: Ensure each of the strings added to the URL are properly escaped.
It looks like nameAdded may be the cause of your problems. Is it nil at that point?
Also
You are allocating a string, setting it to string1 and then immediately setting string1 to the class function stringWithFormat which allocates another string. Also you are using stringWithFormat but you aren't using any format so you could simply use NSString *string1 = #"?nombre=";
Rather than declaring all of those variables you should just use NSMutableString and build it all in one variabl
I have two UITextViews:
self.itemsTextView.text;
self.priceTextView.text;
I want to concatenate these two like so:
NSString *data = self.textView.text + self.itemsTextView.text;
I have tried using a colon, as suggested by this thread, but it doesn't work.
NSString *data = [self.textView.text : self.itemsTextView.text];
For concatenating you have several options :
Using stringWithFormat:
NSString *dataString =[NSString stringWithFormat:#"%#%#",self.textView.text, self.itemsTextView.text];
Using stringByAppendingString:
NSMutableString has appendString:
You may use
NSString * data = [NSString stringWithFormat:#"%#%#",self.textView.text,self.itemsTextView.text];
There are so many ways to do this. In addition to the stringWithFormat: approaches of the other answers you can do (where a and b are other strings):
NSString *c = [a stringByAppendingString:b];
or
NSMutableString *c = [a mutableCopy];
[c appendString b];
I've got a string that shows the stock amount using "-" as separators.
It's built up like this: localStock-wareHouseStock-supplierStock
Now I want to update the supplierStock at the end of the string, but as you can see in the code below it goes wrong when the original string returns more than a single-space value (such as 20).
Is there a way to remove all characters until the last "-" (or remove characters after the second "-")?
NSMutableString *string1 = [NSMutableString stringWithString: p1.colorStock];
NSLog(#"string1: %#",string1);
NSString *newString = [string1 substringToIndex:[string1 length]-2];
NSLog(#"newString: %#",newString);
NSString *colorStock = [NSString stringWithFormat:#"%#-%#",newString,p2.supplierStock];
NSLog(#"colorstock: %#",colorStock);
p1.colorStock = colorStock;
NSLog1
string1: 0-0-0
newString: 0-0
colorstock: 0-0-20
NSLog2
string1: 0-0-20
newString: 0-0-
colorstock: 0-0--20
EDIT: Got it working thanks to Srikar!
NSString *string1 = [NSString stringWithString: p1.colorStock];
NSLog(#"string1: %#",string1);
NSString *finalString = [string1 stringByReplacingOccurrencesOfString:[[string1 componentsSeparatedByString:#"-"] lastObject] withString:p2.supplierStock.stringValue];
NSLog(#"finalString: %#",finalString);
p1.colorStock = finalString;
Why not use componentsSeparatedByString followed by lastObject ?
NSString *supplierStock = [[string1 componentsSeparatedByString:#"-"] lastObject];
The above works if the "stock amount" is always in sets of 3's separated by a "-". Also since you always want supplierStock, lastObject is perfect for your needs.
Of course after splitting string1 with - you get a NSArray instance and you can access the individual components using objectAtIndex:index. So if you want localStock you can get by
NSString *localStock = [[string1 componentsSeparatedByString:#"-"] objectAtIndex:0];
I would suggest splitting the string into the 3 parts using [NSString componentsSeparatedByString:#"-"] and then building it back up again:
NSArray *components = [p1.colorStock componentsSeparatedByString:#"-"];
p1.colorStock = [NSString stringWithFormat:#"%#-%#-%#",
[components objectAtIndex:0],
[components objectAtIndex:1],
p2.supplierStock];
With a string that looks like
NSString *myString = #"Hello-World";
you can separate it with the componentsSeparatedByString: method of the NSString object as
NSArray *myWords = [myString componentsSeparatedByString:#"-"];
The myWords - array will then contain the two NSString objects Hello and World.
To access the strings:
NSString *theHelloString = [myWords objectAtIndex:0];
NSString *theWorldString = [myWords objectAtIndex:1];
Hope it helps!
None of these examples show how to do this if you are unaware of how many of these separator occurrences you're going to have in the original string.
Here's what I believe the correct the correct code should be for dismantling the original string and rebuilding it until you reach the final separator, regardless of how many separators it contains.
NSString *seperator = #" ";
NSString *everythingBeforeLastSeperator;
NSArray *stringComponents = [originalString componentsSeparatedByString:seperator];
if (stringComponents.count!=0) {
everythingBeforeLastSeperator = [stringComponents objectAtIndex:0];
for (int a = 1 ; a < (stringComponents.count - 1) ; a++) {
everythingBeforeLastSeperator = [NSString stringWithFormat:#"%#%#%#", everythingBeforeLastSeperator, seperator, [stringComponents objectAtIndex:a]];
}
}
return everythingBeforeLastSeperator;