FStringUtilities.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 "FirebaseDatabase/Sources/third_party/SocketRocket/NSData+SRB64Additions.h"
  18. #import <CommonCrypto/CommonDigest.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
  25. // strlen will give us.
  26. unsigned long dataLen = strlen(cstr);
  27. NSData *data = [NSData dataWithBytes:cstr length:dataLen];
  28. uint8_t digest[CC_SHA1_DIGEST_LENGTH];
  29. CC_SHA1(data.bytes, (unsigned int)data.length, digest);
  30. NSData *output = [[NSData alloc] initWithBytes:digest
  31. length:CC_SHA1_DIGEST_LENGTH];
  32. return [FSRUtilities base64EncodedStringFromData:output];
  33. }
  34. + (NSString *)urlDecoded:(NSString *)url {
  35. NSString *replaced = [url stringByReplacingOccurrencesOfString:@"+"
  36. withString:@" "];
  37. NSString *decoded = [replaced stringByRemovingPercentEncoding];
  38. // This is kind of a hack, but is generally how the js client works. We
  39. // could run into trouble if some piece is a correctly escaped %-sequence,
  40. // and another isn't. But, that's bad input anyways...
  41. if (decoded) {
  42. return decoded;
  43. } else {
  44. return replaced;
  45. }
  46. }
  47. + (NSString *)urlEncoded:(NSString *)url {
  48. // Didn't seem like there was an Apple NSCharacterSet that had our version
  49. // of the encoding So I made my own, following RFC 2396
  50. // https://www.ietf.org/rfc/rfc2396.txt allowedCharacters = alphanum | "-" |
  51. // "_" | "~"
  52. NSCharacterSet *allowedCharacters = [NSCharacterSet
  53. characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGH"
  54. @"IJKLMNOPQRSTUVWXYZ0123456789-_~"];
  55. return [url
  56. stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
  57. }
  58. + (NSString *)sanitizedForUserAgent:(NSString *)str {
  59. return
  60. [str stringByReplacingOccurrencesOfString:@"/|_"
  61. withString:@"|"
  62. options:NSRegularExpressionSearch
  63. range:NSMakeRange(0, [str length])];
  64. }
  65. @end