Git Product home page Git Product logo

ed25519's Introduction

Ed25519 for Swift 3.x

Swift CircleCI

This project is a Swift adaptation of the portable C Ed25519 implementation available here.
The complete functionality, including the key exchange and scalar addition functions are available.

On both macOS and Linux systems, /dev/urandom will be used for generating seeds.

Installation

To integrate the library in your SwiftPM project, add the following dependency to Package.swift:

.Package(url: "https://github.com/vzsg/ed25519.git", majorVersion: 0, minor: 1)

API

let seed = try Seed()

Creates a 32 byte random seed for key generation.
May throw Ed25519Error.seedGenerationFailed in case there was a problem with reading from /dev/urandom.

let seedBytes: [UInt8] = [ ... ]
let seed2 = try Seed(bytes: bytes)

Creates a seed for key generation with a previously known value.
bytes must be an array of 32 bytes, otherwise Ed25519Error.invalidSeedLength will be thrown.

let keyPair = KeyPair(seed: seed)

Creates a new key pair from the given seed.

let pubBytes: [UInt8] = [ ... ]
let privBytes: [UInt8] = [ ... ]
let keyPair2 = try KeyPair(publicKey: pubBytes, privateKey: privBytes)

Creates a key pair from the previously known public and private keys.
Throws Ed25519Error.invalidPublicKeyLength if the public key is not 32, or Ed25519Error.invalidPrivateKeyLength if the private key is not 64 bytes long.

let publicKey = try PublicKey(pubBytes)
let privateKey = try PrivateKey(privBytes)
let keyPair3 = KeyPair(publicKey: publicKey, privateKey: privateKey)

Construct public and private keys directly.
The same length limitations and exceptions apply.

publicKey.bytes
privateKey.bytes
keyPair.publicKey.bytes
keyPair.privateKey.bytes
seed.bytes

Access raw byte arrays behind any key or seed with the bytes property.

let message: [UInt8] = [ ... ]
let signature: [UInt8] = keyPair.sign(message: message)

Creates a signature of the given message with the given key pair.
signature will be an array of 64 bytes.

let valid: Bool = try keyPair.verify(signature: signature, message: message)
let valid2: Bool = try publicKey.verify(signature: signature, message: message)

Verifies the signature on the given message using a key pair or a public key.
Both methods throw Ed25519Error.invalidSignatureLength if signature is not an array of 64 bytes.

let scalar: [UInt8] = [ ... ]
let keyPairMod = try keyPair.add(scalar: scalar)
let publicKeyMod = try publicKey.add(scalar: scalar)
let privateKeyMod = try privateKey.add(scalar: scalar)

Adds scalar to the given key pair, public or private key, where scalar is a 32 byte buffer (possibly generated with a seed), generating new key(s). This is useful for enforcing randomness on a key pair by a third party while only knowing the public key, among other things. Warning: the last bit of the scalar is ignored - if comparing scalars make sure to ignore scalar[31].

let sharedSecret = keyPair.keyExchange()
let sharedSecret2 = try KeyPair.keyExchange(publicKey: pubBytes, privateKey: privBytes)
let sharedSecret3 = KeyPair.keyExchange(publicKey: publicKey, privateKey: privateKey)

Performs a key exchange on the given public key and private key, producing a shared secret, an array of 32 bytes. It is recommended to hash the shared secret before using it.

Example

Check the unit tests for usage examples.

License

The Swift library is released under the MIT license. See LICENSE for details.

The C implementation is (c) 2015 Orson Peters, licensed under the permissive zlib license.
The original source code is not modified, only reorganized for SwiftPM consumption.
See license.txt in CEd25519 for details.

ed25519's People

Contributors

vzsg avatar sdrpa avatar

Watchers

James Cloos avatar  avatar  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.