FIRCLSByteUtility.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "Crashlytics/Shared/FIRCLSByteUtility.h"
  15. #import <CommonCrypto/CommonDigest.h>
  16. #import <CommonCrypto/CommonHMAC.h>
  17. #pragma mark Private functions
  18. static const char FIRCLSHexMap[] = "0123456789abcdef";
  19. void FIRCLSHexFromByte(uint8_t c, char output[]) {
  20. if (!output) {
  21. return;
  22. }
  23. output[0] = FIRCLSHexMap[c >> 4];
  24. output[1] = FIRCLSHexMap[c & 0x0f];
  25. }
  26. void FIRCLSSafeHexToString(const uint8_t *value, size_t length, char *outputBuffer) {
  27. if (!outputBuffer) {
  28. return;
  29. }
  30. memset(outputBuffer, 0, (length * 2) + 1);
  31. if (!value) {
  32. return;
  33. }
  34. for (size_t i = 0; i < length; ++i) {
  35. uint8_t c = value[i];
  36. FIRCLSHexFromByte(c, &outputBuffer[i * 2]);
  37. }
  38. }
  39. NSString *FIRCLSNSDataPrettyDescription(NSData *data) {
  40. NSString *string;
  41. char *buffer;
  42. size_t size;
  43. NSUInteger length;
  44. // we need 2 hex char for every byte of data, plus one more spot for a
  45. // null terminator
  46. length = data.length;
  47. size = (length * 2) + 1;
  48. buffer = malloc(sizeof(char) * size);
  49. if (!buffer) {
  50. return nil;
  51. }
  52. FIRCLSSafeHexToString(data.bytes, length, buffer);
  53. string = [NSString stringWithUTF8String:buffer];
  54. free(buffer);
  55. return string;
  56. }
  57. #pragma mark Public functions
  58. NSString *FIRCLSHashBytes(const void *bytes, size_t length) {
  59. uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
  60. CC_SHA1(bytes, (CC_LONG)length, digest);
  61. NSData *result = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
  62. return FIRCLSNSDataPrettyDescription(result);
  63. }
  64. NSString *FIRCLSHashNSData(NSData *data) {
  65. return FIRCLSHashBytes(data.bytes, data.length);
  66. }
  67. NSString *FIRCLS256HashBytes(const void *bytes, size_t length) {
  68. uint8_t digest[CC_SHA256_DIGEST_LENGTH] = {0};
  69. CC_SHA256(bytes, (CC_LONG)length, digest);
  70. NSData *result = [NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
  71. return FIRCLSNSDataPrettyDescription(result);
  72. }
  73. NSString *FIRCLS256HashNSData(NSData *data) {
  74. return FIRCLS256HashBytes(data.bytes, data.length);
  75. }
  76. void FIRCLSEnumerateByteRangesOfNSDataUsingBlock(
  77. NSData *data, void (^block)(const void *bytes, NSRange byteRange, BOOL *stop)) {
  78. if ([data respondsToSelector:@selector(enumerateByteRangesUsingBlock:)]) {
  79. [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
  80. block(bytes, byteRange, stop);
  81. }];
  82. return;
  83. }
  84. // Fall back to the less-efficient mechanism for older OSes. Safe
  85. // to ignore the return value of stop, since we'll only ever
  86. // call this once anyways
  87. BOOL stop = NO;
  88. block(data.bytes, NSMakeRange(0, data.length), &stop);
  89. }