GithubHelp home page GithubHelp logo

lszabo4 / swiftdigest Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nikolairuhe/swiftdigest

0.0 0.0 0.0 26 KB

Simple md5 implementation in pure Swift with no dependencies.

License: MIT License

Swift 100.00%

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

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.