SRHash.m 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // Copyright (c) 2016-present, Facebook, Inc.
  3. // All rights reserved.
  4. //
  5. // This source code is licensed under the BSD-style license found in the
  6. // LICENSE file in the root directory of this source tree. An additional grant
  7. // of patent rights can be found in the PATENTS file in the same directory.
  8. //
  9. #import "SRHash.h"
  10. #import <CommonCrypto/CommonDigest.h>
  11. NS_ASSUME_NONNULL_BEGIN
  12. NSData *SRSHA1HashFromString(NSString *string)
  13. {
  14. size_t length = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  15. return SRSHA1HashFromBytes(string.UTF8String, length);
  16. }
  17. NSData *SRSHA1HashFromBytes(const char *bytes, size_t length)
  18. {
  19. uint8_t outputLength = CC_SHA1_DIGEST_LENGTH;
  20. unsigned char output[outputLength];
  21. CC_SHA1(bytes, (CC_LONG)length, output);
  22. return [NSData dataWithBytes:output length:outputLength];
  23. }
  24. NSString *SRBase64EncodedStringFromData(NSData *data)
  25. {
  26. if ([data respondsToSelector:@selector(base64EncodedStringWithOptions:)]) {
  27. return [data base64EncodedStringWithOptions:0];
  28. }
  29. #pragma clang diagnostic push
  30. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  31. return [data base64Encoding];
  32. #pragma clang diagnostic pop
  33. }
  34. NS_ASSUME_NONNULL_END