Crypto: Swift wrapper for the Curve25519 C code
Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright © 2018 WireGuard LLC. All rights reserved.
|
||||
|
||||
import UIKit
|
||||
|
||||
struct Curve25519 {
|
||||
static func generatePrivateKey() -> Data {
|
||||
var privateKey = Data(repeating: 0, count: 32)
|
||||
privateKey.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer<UInt8>) in
|
||||
curve25519_generate_private_key(bytes)
|
||||
}
|
||||
assert(privateKey.count == 32)
|
||||
return privateKey
|
||||
}
|
||||
|
||||
static func generatePublicKey(fromPrivateKey privateKey: Data) -> Data {
|
||||
assert(privateKey.count == 32)
|
||||
var publicKey = Data(repeating: 0, count: 32)
|
||||
privateKey.withUnsafeBytes { (privateKeyBytes: UnsafePointer<UInt8>) in
|
||||
publicKey.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer<UInt8>) in
|
||||
curve25519_derive_public_key(bytes, privateKeyBytes)
|
||||
}
|
||||
}
|
||||
assert(publicKey.count == 32)
|
||||
return publicKey
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user