FStringUtilities.m 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 "FirebaseDatabase/Sources/Utilities/FStringUtilities.h"
  17. #import <CommonCrypto/CommonDigest.h>
  18. @implementation FStringUtilities
  19. // http://stackoverflow.com/questions/3468268/objective-c-sha1
  20. // http://stackoverflow.com/questions/7310457/ios-objective-c-sha-1-and-base64-problem
  21. + (NSString *)base64EncodedSha1:(NSString *)str {
  22. const char *cstr = [str cStringUsingEncoding:NSUTF8StringEncoding];
  23. // NSString reports length in characters, but we want it in bytes, which
  24. // 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
  30. length:CC_SHA1_DIGEST_LENGTH];
  31. return [output base64EncodedStringWithOptions:0];
  32. }
  33. + (NSString *)urlDecoded:(NSString *)url {
  34. NSString *replaced = [url stringByReplacingOccurrencesOfString:@"+"
  35. withString:@" "];
  36. NSString *decoded = [replaced stringByRemovingPercentEncoding];
  37. // This is kind of a hack, but is generally how the js client works. We
  38. // could run into trouble if some piece is a correctly escaped %-sequence,
  39. // and another isn't. But, that's bad input anyways...
  40. if (decoded) {
  41. return decoded;
  42. } else {
  43. return replaced;
  44. }
  45. }
  46. + (NSString *)urlEncoded:(NSString *)url {
  47. // Didn't seem like there was an Apple NSCharacterSet that had our version
  48. // of the encoding So I made my own, following RFC 2396
  49. // https://www.ietf.org/rfc/rfc2396.txt allowedCharacters = alphanum | "-" |
  50. // "_" | "~"
  51. NSCharacterSet *allowedCharacters = [NSCharacterSet
  52. characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGH"
  53. @"IJKLMNOPQRSTUVWXYZ0123456789-_~"];
  54. return [url
  55. stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
  56. }
  57. + (NSString *)sanitizedForUserAgent:(NSString *)str {
  58. return
  59. [str stringByReplacingOccurrencesOfString:@"/|_"
  60. withString:@"|"
  61. options:NSRegularExpressionSearch
  62. range:NSMakeRange(0, [str length])];
  63. }
  64. @end