Skip to content

Commit

Permalink
Updated to Swift 3
Browse files Browse the repository at this point in the history
  • Loading branch information
mpurland committed Mar 4, 2017
1 parent a811333 commit 5a9124e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
13 changes: 12 additions & 1 deletion SwiftMD5.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,16 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 0730;
LastUpgradeCheck = 0820;
ORGANIZATIONNAME = "Matthew Purland";
TargetAttributes = {
DE5D68111CA9C4AC001154B6 = {
CreatedOnToolsVersion = 7.3;
LastSwiftMigration = 0820;
};
DE5D681B1CA9C4AC001154B6 = {
CreatedOnToolsVersion = 7.3;
LastSwiftMigration = 0820;
};
};
};
Expand Down Expand Up @@ -234,8 +236,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
Expand Down Expand Up @@ -282,8 +286,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
Expand All @@ -303,6 +309,7 @@
MACOSX_DEPLOYMENT_TARGET = 10.11;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand All @@ -325,6 +332,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -344,6 +352,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.github.mpurland.SwiftMD5;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -355,6 +364,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.github.mpurland.SwiftMD5Tests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -366,6 +376,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.github.mpurland.SwiftMD5Tests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0730"
LastUpgradeVersion = "0820"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
18 changes: 9 additions & 9 deletions SwiftMD5/SwiftMD5.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ public struct Digest {
}

public var checksum: String {
return encodeMD5Digest(digest)
return encodeMD5(digest: digest)
}
}

private func F(b: Word, _ c: Word, _ d: Word) -> Word {
private func F(_ b: Word, _ c: Word, _ d: Word) -> Word {
return (b & c) | ((~b) & d)
}

private func G(b: Word, _ c: Word, _ d: Word) -> Word {
private func G(_ b: Word, _ c: Word, _ d: Word) -> Word {
return (b & d) | (c & (~d))
}

private func H(b: Word, _ c: Word, _ d: Word) -> Word {
private func H(_ b: Word, _ c: Word, _ d: Word) -> Word {
return b ^ c ^ d
}

private func I(b: Word, _ c: Word, _ d: Word) -> Word {
private func I(_ b: Word, _ c: Word, _ d: Word) -> Word {
return c ^ (b | (~d))
}

private func rotateLeft(x: Word, by: Word) -> Word {
private func rotateLeft(_ x: Word, by: Word) -> Word {
return ((x << by) & 0xFFFFFFFF) | (x >> (32 - by))
}

// MARK: - Calculating a MD5 digest of bytes from bytes

public func md5(bytes: [Byte]) -> Digest {
public func md5(_ bytes: [Byte]) -> Digest {
// Initialization
let s: [Word] = [
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
Expand Down Expand Up @@ -199,7 +199,7 @@ public func md5(bytes: [Byte]) -> Digest {

// MARK: - Encoding a MD5 digest of bytes to a string

public func encodeMD5Digest(digest: [Byte]) -> String {
public func encodeMD5(digest: [Byte]) -> String {
assert(digest.count == 16)

let str = digest.reduce("") { str, byte in
Expand All @@ -217,7 +217,7 @@ public func encodeMD5Digest(digest: [Byte]) -> String {

extension String {
public var md5: String {
return encodeMD5Digest(md5Digest)
return encodeMD5(digest: md5Digest)
}

public var md5Digest: [Byte] {
Expand Down
10 changes: 5 additions & 5 deletions SwiftMD5Tests/SwiftMD5Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class SwiftMD5Tests: XCTestCase {
}

func testEncodeMD5Digest() {
XCTAssertEqual("The quick brown fox jumps over the lazy dog".md5, encodeMD5Digest([158, 16, 125, 157, 55, 43, 182, 130, 107, 216, 29, 53, 66, 164, 25, 214]))
XCTAssertEqual("The quick brown fox jumps over the lazy dog.".md5, encodeMD5Digest([228, 217, 9, 194, 144, 208, 251, 28, 160, 104, 255, 173, 223, 34, 203, 208]))
XCTAssertEqual("".md5, encodeMD5Digest([212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 248, 66, 126]))
XCTAssertEqual("md5".md5, encodeMD5Digest([27, 194, 155, 54, 246, 35, 186, 130, 170, 246, 114, 79, 211, 177, 103, 24]))
XCTAssertEqual("abc".md5, encodeMD5Digest([144, 1, 80, 152, 60, 210, 79, 176, 214, 150, 63, 125, 40, 225, 127, 114]))
XCTAssertEqual("The quick brown fox jumps over the lazy dog".md5, encodeMD5(digest: [158, 16, 125, 157, 55, 43, 182, 130, 107, 216, 29, 53, 66, 164, 25, 214]))
XCTAssertEqual("The quick brown fox jumps over the lazy dog.".md5, encodeMD5(digest: [228, 217, 9, 194, 144, 208, 251, 28, 160, 104, 255, 173, 223, 34, 203, 208]))
XCTAssertEqual("".md5, encodeMD5(digest: [212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 248, 66, 126]))
XCTAssertEqual("md5".md5, encodeMD5(digest: [27, 194, 155, 54, 246, 35, 186, 130, 170, 246, 114, 79, 211, 177, 103, 24]))
XCTAssertEqual("abc".md5, encodeMD5(digest: [144, 1, 80, 152, 60, 210, 79, 176, 214, 150, 63, 125, 40, 225, 127, 114]))
}

func testMD5Digest() {
Expand Down

0 comments on commit 5a9124e

Please sign in to comment.