Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting the string to a SHA256 hash #89

Merged
merged 2 commits into from
Jun 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dwyl/TasksTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class TasksTableViewController: UITableViewController, WCSessionDelegate {
if ProjectTimer.sharedInstance.isTimerRunning() {
print("cant start timer, another one is already running in \(ProjectTimer.sharedInstance.projectName)")
} else {
ProjectTimer.sharedInstance.startTimer()
ProjectTimer.sharedInstance.startTimer(name: (message["startTimerFor"] as! String?)!)
ProjectTimer.sharedInstance.projectName = message["startTimerFor"] as! String
}

Expand Down
12 changes: 12 additions & 0 deletions dwyl/dwyl.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
0D2038421E6DA9EA008CB3F4 /* TasksTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D2038411E6DA9EA008CB3F4 /* TasksTableViewCell.swift */; };
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 @@ -125,6 +127,9 @@
0D2038411E6DA9EA008CB3F4 /* TasksTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TasksTableViewCell.swift; sourceTree = "<group>"; };
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 @@ -190,6 +195,8 @@
0DCC075B1E9BA9C400868547 /* formatTime.swift */,
0D243CD11EAE192700053265 /* materialColors.swift */,
0DB2792C1EC4B2350011559E /* ProjectTimer.swift */,
0D24BB901EFC0F8B009D2334 /* dateConverter.swift */,
0D24BB951EFC292D009D2334 /* stringToHash.swift */,
);
name = lib;
sourceTree = "<group>";
Expand Down Expand Up @@ -248,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 @@ -546,6 +554,8 @@
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 */,
0D1D193A1E796B36003C9B39 /* TaskListTableViewCell.swift in Sources */,
Expand Down Expand Up @@ -751,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 @@ -771,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
20 changes: 18 additions & 2 deletions dwyl/time/ProjectTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ProjectTimer {
do {
let RunningProject = try managedObjectContext!.fetch(fetchRequest)
if RunningProject.count == 1 {
startTimer()
startTimer(name: (RunningProject.first?.project_name)!)
projectName = (RunningProject.first?.project_name)!
startDate = (RunningProject.first?.task_start_date as Date?)!
updateTimer()
Expand All @@ -41,12 +41,28 @@ class ProjectTimer {
}
}

func startTimer() {
func startTimer(name: String) {
timerRunning = true
startDate = Date()
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)

// create a json object.
do {
// let jsonData = try JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted)
let convertedDate = dateToString(date: startDate)
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

let hashedString = StringToHash(jsonToString!)
print(hashedString ?? "no data")
}
catch {
print(error.localizedDescription)
}
}



func stopTimer() {
startDate = Date()
timer.invalidate()
Expand Down
2 changes: 1 addition & 1 deletion dwyl/time/ViewTaskViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class ViewTaskViewController: UIViewController, UITableViewDataSource, UITableVi
let start_time = Date()
let project_name = self.project_name

ProjectTimer.sharedInstance.startTimer()
ProjectTimer.sharedInstance.startTimer(name: project_name)
playButton.isEnabled = false
ProjectTimer.sharedInstance.projectName = project_name
createTask(project_name: project_name, start_time: start_time)
Expand Down
21 changes: 21 additions & 0 deletions dwyl/time/dateConverter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// dateConverter.swift
// dwyl
//
// Created by Sohil Pandya on 22/06/2017.
// Copyright © 2017 dwyl. All rights reserved.
//

import Foundation

func dateToString(date: Date) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
return formatter.string(from: date)
}

func stringToDate(string: String) -> Date {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
return formatter.date(from: string)!
}
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? {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sohilpandya this looks good. we're going to need to write a test to check for the output at some point soon but for now this is fine. thanks! 👍

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
}