| 1 | 
           efrain | 
           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 | 
              | 
        
        
            | 
            | 
           15 | 
           #import "Crashlytics/Shared/FIRCLSByteUtility.h"
  | 
        
        
            | 
            | 
           16 | 
              | 
        
        
            | 
            | 
           17 | 
           #import <CommonCrypto/CommonDigest.h>
  | 
        
        
            | 
            | 
           18 | 
           #import <CommonCrypto/CommonHMAC.h>
  | 
        
        
            | 
            | 
           19 | 
              | 
        
        
            | 
            | 
           20 | 
           #pragma mark Private functions
  | 
        
        
            | 
            | 
           21 | 
              | 
        
        
            | 
            | 
           22 | 
           static const char FIRCLSHexMap[] = "0123456789abcdef";
  | 
        
        
            | 
            | 
           23 | 
              | 
        
        
            | 
            | 
           24 | 
           void FIRCLSHexFromByte(uint8_t c, char output[]) {
  | 
        
        
            | 
            | 
           25 | 
             if (!output) {
  | 
        
        
            | 
            | 
           26 | 
               return;
  | 
        
        
            | 
            | 
           27 | 
             }
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
             output[0] = FIRCLSHexMap[c >> 4];
  | 
        
        
            | 
            | 
           30 | 
             output[1] = FIRCLSHexMap[c & 0x0f];
  | 
        
        
            | 
            | 
           31 | 
           }
  | 
        
        
            | 
            | 
           32 | 
              | 
        
        
            | 
            | 
           33 | 
           void FIRCLSSafeHexToString(const uint8_t *value, size_t length, char *outputBuffer) {
  | 
        
        
            | 
            | 
           34 | 
             if (!outputBuffer) {
  | 
        
        
            | 
            | 
           35 | 
               return;
  | 
        
        
            | 
            | 
           36 | 
             }
  | 
        
        
            | 
            | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
             memset(outputBuffer, 0, (length * 2) + 1);
  | 
        
        
            | 
            | 
           39 | 
              | 
        
        
            | 
            | 
           40 | 
             if (!value) {
  | 
        
        
            | 
            | 
           41 | 
               return;
  | 
        
        
            | 
            | 
           42 | 
             }
  | 
        
        
            | 
            | 
           43 | 
              | 
        
        
            | 
            | 
           44 | 
             for (size_t i = 0; i < length; ++i) {
  | 
        
        
            | 
            | 
           45 | 
               uint8_t c = value[i];
  | 
        
        
            | 
            | 
           46 | 
              | 
        
        
            | 
            | 
           47 | 
               FIRCLSHexFromByte(c, &outputBuffer[i * 2]);
  | 
        
        
            | 
            | 
           48 | 
             }
  | 
        
        
            | 
            | 
           49 | 
           }
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
           NSString *FIRCLSNSDataPrettyDescription(NSData *data) {
  | 
        
        
            | 
            | 
           52 | 
             NSString *string;
  | 
        
        
            | 
            | 
           53 | 
             char *buffer;
  | 
        
        
            | 
            | 
           54 | 
             size_t size;
  | 
        
        
            | 
            | 
           55 | 
             NSUInteger length;
  | 
        
        
            | 
            | 
           56 | 
              | 
        
        
            | 
            | 
           57 | 
             // we need 2 hex char for every byte of data, plus one more spot for a
  | 
        
        
            | 
            | 
           58 | 
             // null terminator
  | 
        
        
            | 
            | 
           59 | 
             length = data.length;
  | 
        
        
            | 
            | 
           60 | 
             size = (length * 2) + 1;
  | 
        
        
            | 
            | 
           61 | 
             buffer = malloc(sizeof(char) * size);
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
             if (!buffer) {
  | 
        
        
            | 
            | 
           64 | 
               return nil;
  | 
        
        
            | 
            | 
           65 | 
             }
  | 
        
        
            | 
            | 
           66 | 
              | 
        
        
            | 
            | 
           67 | 
             FIRCLSSafeHexToString(data.bytes, length, buffer);
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
             string = [NSString stringWithUTF8String:buffer];
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
             free(buffer);
  | 
        
        
            | 
            | 
           72 | 
              | 
        
        
            | 
            | 
           73 | 
             return string;
  | 
        
        
            | 
            | 
           74 | 
           }
  | 
        
        
            | 
            | 
           75 | 
              | 
        
        
            | 
            | 
           76 | 
           #pragma mark Public functions
  | 
        
        
            | 
            | 
           77 | 
              | 
        
        
            | 
            | 
           78 | 
           NSString *FIRCLSHashBytes(const void *bytes, size_t length) {
  | 
        
        
            | 
            | 
           79 | 
             uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
  | 
        
        
            | 
            | 
           80 | 
             CC_SHA1(bytes, (CC_LONG)length, digest);
  | 
        
        
            | 
            | 
           81 | 
              | 
        
        
            | 
            | 
           82 | 
             NSData *result = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
             return FIRCLSNSDataPrettyDescription(result);
  | 
        
        
            | 
            | 
           85 | 
           }
  | 
        
        
            | 
            | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
           NSString *FIRCLSHashNSData(NSData *data) {
  | 
        
        
            | 
            | 
           88 | 
             return FIRCLSHashBytes(data.bytes, data.length);
  | 
        
        
            | 
            | 
           89 | 
           }
  | 
        
        
            | 
            | 
           90 | 
              | 
        
        
            | 
            | 
           91 | 
           NSString *FIRCLS256HashBytes(const void *bytes, size_t length) {
  | 
        
        
            | 
            | 
           92 | 
             uint8_t digest[CC_SHA256_DIGEST_LENGTH] = {0};
  | 
        
        
            | 
            | 
           93 | 
             CC_SHA256(bytes, (CC_LONG)length, digest);
  | 
        
        
            | 
            | 
           94 | 
              | 
        
        
            | 
            | 
           95 | 
             NSData *result = [NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
  | 
        
        
            | 
            | 
           96 | 
              | 
        
        
            | 
            | 
           97 | 
             return FIRCLSNSDataPrettyDescription(result);
  | 
        
        
            | 
            | 
           98 | 
           }
  | 
        
        
            | 
            | 
           99 | 
              | 
        
        
            | 
            | 
           100 | 
           NSString *FIRCLS256HashNSData(NSData *data) {
  | 
        
        
            | 
            | 
           101 | 
             return FIRCLS256HashBytes(data.bytes, data.length);
  | 
        
        
            | 
            | 
           102 | 
           }
  | 
        
        
            | 
            | 
           103 | 
              | 
        
        
            | 
            | 
           104 | 
           void FIRCLSEnumerateByteRangesOfNSDataUsingBlock(
  | 
        
        
            | 
            | 
           105 | 
               NSData *data, void (^block)(const void *bytes, NSRange byteRange, BOOL *stop)) {
  | 
        
        
            | 
            | 
           106 | 
             if ([data respondsToSelector:@selector(enumerateByteRangesUsingBlock:)]) {
  | 
        
        
            | 
            | 
           107 | 
               [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
  | 
        
        
            | 
            | 
           108 | 
                 block(bytes, byteRange, stop);
  | 
        
        
            | 
            | 
           109 | 
               }];
  | 
        
        
            | 
            | 
           110 | 
              | 
        
        
            | 
            | 
           111 | 
               return;
  | 
        
        
            | 
            | 
           112 | 
             }
  | 
        
        
            | 
            | 
           113 | 
              | 
        
        
            | 
            | 
           114 | 
             // Fall back to the less-efficient mechanism for older OSes. Safe
  | 
        
        
            | 
            | 
           115 | 
             // to ignore the return value of stop, since we'll only ever
  | 
        
        
            | 
            | 
           116 | 
             // call this once anyways
  | 
        
        
            | 
            | 
           117 | 
             BOOL stop = NO;
  | 
        
        
            | 
            | 
           118 | 
              | 
        
        
            | 
            | 
           119 | 
             block(data.bytes, NSMakeRange(0, data.length), &stop);
  | 
        
        
            | 
            | 
           120 | 
           }
  |