Skip to content

Commit

Permalink
Merge pull request #89 from dwyl/sha256-hash
Browse files Browse the repository at this point in the history
Converting the string to a SHA256 hash
  • Loading branch information
iteles authored Jun 23, 2017
2 parents 17f6e23 + 4cd257b commit 94fe853
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
8 changes: 8 additions & 0 deletions dwyl/dwyl.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
0D2038441E70180C008CB3F4 /* AddTaskViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D2038431E70180C008CB3F4 /* AddTaskViewController.swift */; };
0D243CD21EAE192700053265 /* materialColors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D243CD11EAE192700053265 /* materialColors.swift */; };
0D24BB911EFC0F8B009D2334 /* dateConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D24BB901EFC0F8B009D2334 /* dateConverter.swift */; };
0D24BB961EFC292D009D2334 /* stringToHash.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D24BB951EFC292D009D2334 /* stringToHash.swift */; };
0D42F4061EEFFD1D00471321 /* ProjectTimerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D42F4051EEFFD1D00471321 /* ProjectTimerTest.swift */; };
0D5AA1451E71881E002D87C3 /* TaskCoreData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D5AA1441E71881E002D87C3 /* TaskCoreData.swift */; };
0D662CE21E70537900AFDE32 /* Task.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D662CE11E70537900AFDE32 /* Task.swift */; };
Expand Down Expand Up @@ -127,6 +128,8 @@
0D2038431E70180C008CB3F4 /* AddTaskViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AddTaskViewController.swift; path = ../AddTaskViewController.swift; sourceTree = "<group>"; };
0D243CD11EAE192700053265 /* materialColors.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = materialColors.swift; sourceTree = "<group>"; };
0D24BB901EFC0F8B009D2334 /* dateConverter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = dateConverter.swift; sourceTree = "<group>"; };
0D24BB941EFC2832009D2334 /* dwyl-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "dwyl-Bridging-Header.h"; sourceTree = "<group>"; };
0D24BB951EFC292D009D2334 /* stringToHash.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = stringToHash.swift; sourceTree = "<group>"; };
0D42F4051EEFFD1D00471321 /* ProjectTimerTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProjectTimerTest.swift; sourceTree = "<group>"; };
0D5AA1441E71881E002D87C3 /* TaskCoreData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TaskCoreData.swift; sourceTree = "<group>"; };
0D662CE11E70537900AFDE32 /* Task.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Task.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -193,6 +196,7 @@
0D243CD11EAE192700053265 /* materialColors.swift */,
0DB2792C1EC4B2350011559E /* ProjectTimer.swift */,
0D24BB901EFC0F8B009D2334 /* dateConverter.swift */,
0D24BB951EFC292D009D2334 /* stringToHash.swift */,
);
name = lib;
sourceTree = "<group>";
Expand Down Expand Up @@ -251,6 +255,7 @@
0D2038141E6D82FD008CB3F4 /* LaunchScreen.storyboard */,
0D2038171E6D82FD008CB3F4 /* Info.plist */,
0D5AA1441E71881E002D87C3 /* TaskCoreData.swift */,
0D24BB941EFC2832009D2334 /* dwyl-Bridging-Header.h */,
);
name = dwyl;
path = time;
Expand Down Expand Up @@ -549,6 +554,7 @@
0D2038111E6D82FD008CB3F4 /* time.xcdatamodeld in Sources */,
0DB2792D1EC4B2350011559E /* ProjectTimer.swift in Sources */,
0D1D19371E795B6F003C9B39 /* Project+CoreDataClass.swift in Sources */,
0D24BB961EFC292D009D2334 /* stringToHash.swift in Sources */,
0D24BB911EFC0F8B009D2334 /* dateConverter.swift in Sources */,
0D11C4721E96A15200689D48 /* setNavbarLogo.swift in Sources */,
0D243CD21EAE192700053265 /* materialColors.swift in Sources */,
Expand Down Expand Up @@ -755,6 +761,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.dwyl.time;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "./time/dwyl-Bridging-Header.h";
SWIFT_VERSION = 3.0;
};
name = Debug;
Expand All @@ -775,6 +782,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.dwyl.time;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "./time/dwyl-Bridging-Header.h";
SWIFT_VERSION = 3.0;
};
name = Release;
Expand Down
3 changes: 2 additions & 1 deletion dwyl/time/ProjectTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class ProjectTimer {
let jsonData = try JSONSerialization.data(withJSONObject: ["project_name": name, "start_date": convertedDate, "isTaskRunning": true], options: .prettyPrinted)
let jsonToString = String(data: jsonData, encoding: String.Encoding.utf8) // the data will be converted to the string

print(jsonToString ?? "no data")
let hashedString = StringToHash(jsonToString!)
print(hashedString ?? "no data")
}
catch {
print(error.localizedDescription)
Expand Down
14 changes: 14 additions & 0 deletions dwyl/time/dwyl-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// dwyl-Bridging-Header.h
// dwyl
//
// Created by Sohil Pandya on 22/06/2017.
// Copyright © 2017 dwyl. All rights reserved.
//

#ifndef dwyl_Bridging_Header_h
#define dwyl_Bridging_Header_h

#import "CommonCrypto/CommonCrypto.h"

#endif /* dwyl_Bridging_Header_h */
24 changes: 24 additions & 0 deletions dwyl/time/stringToHash.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// stringToHash.swift
// dwyl
//
// Created by Sohil Pandya on 22/06/2017.
// Copyright © 2017 dwyl. All rights reserved.
//

import Foundation

func sha256(_ data: Data) -> Data? {
guard let res = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH)) else { return nil }
CC_SHA256((data as NSData).bytes, CC_LONG(data.count), res.mutableBytes.assumingMemoryBound(to: UInt8.self))
return res as Data
}

func StringToHash(_ str: String) -> String? {
guard
let data = str.data(using: String.Encoding.utf8),
let shaData = sha256(data)
else { return nil }
let rc = shaData.base64EncodedString(options: [])
return rc
}

0 comments on commit 94fe853

Please sign in to comment.