Skip to content

Instantly share code, notes, and snippets.

@kean
Created April 24, 2018 12:35

Revisions

  1. kean created this gist Apr 24, 2018.
    5 changes: 5 additions & 0 deletions NukeDemo-Bridging-Header.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    //
    // Use this file to import your target's public headers that you would like to expose to Swift.
    //

    #import "SHA1.h"
    11 changes: 11 additions & 0 deletions SHA1.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    // The MIT License (MIT)
    //
    // Copyright (c) 2015-2018 Alexander Grebenyuk (github.com/kean).

    #import <Foundation/Foundation.h>

    /// Produces 160-bit hash value using SHA-1 algorithm.
    /// - returns: String containing 160-bit hash value expressed as a 40 digit
    /// hexadecimal number.
    extern NSString *
    _nuke_sha1(const char *data, uint32_t length);
    20 changes: 20 additions & 0 deletions SHA1.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    // The MIT License (MIT)
    //
    // Copyright (c) 2015-2018 Alexander Grebenyuk (github.com/kean).

    #import <Foundation/Foundation.h>
    #import <CommonCrypto/CommonCrypto.h>

    NSString *
    _nuke_sha1(const char *data, uint32_t length) {
    unsigned char hash[CC_SHA1_DIGEST_LENGTH];
    CC_SHA1(data, (CC_LONG)length, hash);

    char utf8[2 * CC_SHA1_DIGEST_LENGTH + 1];
    char *temp = utf8;
    for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
    snprintf(temp, 3, "%02x", hash[i]);
    temp += 2;
    }
    return [NSString stringWithUTF8String:utf8];
    }