Giter VIP home page Giter VIP logo

swiftdigest's Introduction

SwiftDigest

Copyright (c) 2017 Nikolai Ruhe.

SwiftDigest is released under the MIT License.

Contents

This is a pure Swift implementation of the MD5 algorithm. I might add more algorithms in the future. Or not.

The main purpose is to provide hashing through a pure Swift framework without dependencies other than Swift Foundation. Currently no effort has been taken to optimze the performance. When hashing more than a couple of kilo bytes it might be better to use Apple's CommonCrypto implementation.

Examples

Hash some Data:

let data = Data()
let digest = data.md5
print("md5: \(digest)")

// prints: "md5: d41d8cd98f00b204e9800998ecf8427e"

Hash String contents:

let input = "The quick brown fox jumps over the lazy dog"
let digest = input.utf8.md5
print("md5: \(digest)")

// prints: "md5: 9e107d9d372bb6826bd81d3542a419d6"

Hash the main executable:

let appID = try! Data(contentsOf: Bundle.main.executableURL!).md5
// can be used to send a unique id of the app version to a server or so.

Features

The MD5Digest type is ...

  • Hashable, so it can be used as a key in dictionaries
  • RawRepresentable to convert to and from string representations
  • CustomStringConvertible to make printing easy
  • Codable to enable JSON and Plist coding of types containing a digest property

Interface

/// Represents a 16 byte digest value, created from hashing arbitrary data.
public struct MD5Digest : Hashable, RawRepresentable, CustomStringConvertible, Codable {

    /// Perform hashing of the supplied data.
    public init(from input: Data)

    /// Create a digest from reading a hex representation from the supplied string.
    public init?(rawValue: String)

    /// The 32 digit hex representation.
    public var rawValue: String { get }

    /// The 32 digit hex representation.
    public var description: String { get }

    /// The raw bytes of the digest value, always exactly 16 bytes.
    public var data: Data { get }

    /// The raw bytes of the digest value as a tuple.
    public var bytes: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) { get }
}


public extension Data {

    /// Computes md5 digest value of the contained bytes.
    public var md5: MD5Digest { get }
}

public extension String.UTF8View {

    /// Computes md5 digest value of the string's UTF-8 representation.
    public var md5: MD5Digest { get }
}

swiftdigest's People

Contributors

nikolairuhe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

swiftdigest's Issues

Integer overflows

building for Ipad MC775ZP/A with IOS version 9.3.5 (13G36) is failed with errors:

Integer literal '4023233417' overflows when store into 'UInt32'
Integer literal '2562383102' overflows when store into 'UInt32'

at lines:

var b = UInt32(0xefcdab89)
var c = UInt32(0x98badcfe)

Workaround for stupid Xcode 9.2 hard errors?

In MD5Digest, Xcode 9.2 is flagging these two lines as exceeding the size of a UInt32, which is of course totally wrong:

var b = UInt32(0xefcdab89)
var c = UInt32(0x98badcfe)

Well, I found a workaround (untested at this point):

var b = UInt32(0x70000000) + UInt32(0x7fcdab89) // 0xefcdab89
var c = UInt32(0x40000000) + UInt32(0x58badcfe) // 0x98badcfe

Geez ... [Post is really to help out any other poor soul running into this issue...]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.