FStringUtilities.m 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <CommonCrypto/CommonDigest.h>
  17. #import "FStringUtilities.h"
  18. #import "NSData+SRB64Additions.h"
  19. @implementation FStringUtilities
  20. // http://stackoverflow.com/questions/3468268/objective-c-sha1
  21. // http://stackoverflow.com/questions/7310457/ios-objective-c-sha-1-and-base64-problem
  22. + (NSString *) base64EncodedSha1:(NSString *)str {
  23. const char *cstr = [str cStringUsingEncoding:NSUTF8StringEncoding];
  24. // NSString reports length in characters, but we want it in bytes, which strlen will give us.
  25. unsigned long dataLen = strlen(cstr);
  26. NSData *data = [NSData dataWithBytes:cstr length:dataLen];
  27. uint8_t digest[CC_SHA1_DIGEST_LENGTH];
  28. CC_SHA1(data.bytes, (unsigned int)data.length, digest);
  29. NSData* output = [[NSData alloc] initWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
  30. return [FSRUtilities base64EncodedStringFromData:output];
  31. }
  32. + (NSString *) urlDecoded:(NSString *)url {
  33. NSString* replaced = [url stringByReplacingOccurrencesOfString:@"+" withString:@" "];
  34. NSString* decoded = [replaced stringByRemovingPercentEncoding];
  35. // This is kind of a hack, but is generally how the js client works. We could run into trouble if
  36. // some piece is a correctly escaped %-sequence, and another isn't. But, that's bad input anyways...
  37. if (decoded) {
  38. return decoded;
  39. } else {
  40. return replaced;
  41. }
  42. }
  43. + (NSString *) urlEncoded:(NSString *)url {
  44. // Didn't seem like there was an Apple NSCharacterSet that had our version of the encoding
  45. // So I made my own, following RFC 2396 https://www.ietf.org/rfc/rfc2396.txt
  46. // allowedCharacters = alphanum | "-" | "_" | "~"
  47. NSCharacterSet *allowedCharacters = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_~"];
  48. return [url stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
  49. }
  50. + (NSString *) sanitizedForUserAgent:(NSString *)str {
  51. return [str stringByReplacingOccurrencesOfString:@"/|_" withString:@"|" options:NSRegularExpressionSearch range:NSMakeRange(0, [str length])];
  52. }
  53. @end