Commit e81b0c9d authored by Drew's avatar Drew

Human-readable support for PublicKey

In some cases (e.g. the Nitrogen File Demo) we need a human-readable
storage mechanism for public keys.

We provide such a mechanism here.
parent c8ae8e14
Pipeline #500 failed with stage
......@@ -85,6 +85,7 @@
3AAAC7B51B80AB40009431FC /* NaOH.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3AAAC7AA1B80AB40009431FC /* NaOH.framework */; };
3AC7C1BF1BC157A20036FABB /* memcmp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AC7C1BE1BC157A20036FABB /* memcmp.swift */; };
3AC7C1C11BC1594D0036FABB /* MemCmpTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AC7C1C01BC1594D0036FABB /* MemCmpTests.swift */; };
3AD35A341C14E3FB0095BFB4 /* PublicKey+Human.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AD35A331C14E3FB0095BFB4 /* PublicKey+Human.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
......@@ -179,6 +180,7 @@
3AAAC7BB1B80AB40009431FC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3AC7C1BE1BC157A20036FABB /* memcmp.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = memcmp.swift; sourceTree = "<group>"; };
3AC7C1C01BC1594D0036FABB /* MemCmpTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MemCmpTests.swift; sourceTree = "<group>"; };
3AD35A331C14E3FB0095BFB4 /* PublicKey+Human.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "PublicKey+Human.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
......@@ -237,6 +239,7 @@
3A2AF84B1B95B3A400A3BEF3 /* crypto_stream_chacha20.swift */,
3A8ED35B1BB27E6E00164375 /* Random.swift */,
3AC7C1BE1BC157A20036FABB /* memcmp.swift */,
3AD35A331C14E3FB0095BFB4 /* PublicKey+Human.swift */,
);
path = NaOH;
sourceTree = "<group>";
......@@ -518,6 +521,7 @@
3A19221B1B8127720092C6B1 /* Key+EncryptedWithPublicKey.swift in Sources */,
3A526F6D1B80AC130092BA60 /* crypto_secretbox.swift in Sources */,
3A1922081B8110660092C6B1 /* crypto_box.swift in Sources */,
3AD35A341C14E3FB0095BFB4 /* PublicKey+Human.swift in Sources */,
3A1922021B8107330092C6B1 /* Key+File.swift in Sources */,
3AC7C1BF1BC157A20036FABB /* memcmp.swift in Sources */,
3A526F6B1B80AC130092BA60 /* SwiftAPI.swift in Sources */,
......
//
// PublicKey+Human.swift
// NaOH
//
// This file is part of NaOH. It is subject to the license terms in the LICENSE
// file found in the top level of this distribution
// No part of NaOH, including this file, may be copied, modified,
// propagated, or distributed except according to the terms contained
// in the LICENSE file.
import Foundation
extension PublicKey: CustomStringConvertible {
public var humanReadable: String {
get {
return NSData(bytes: bytes, length: bytes.count).base64EncodedStringWithOptions(NSDataBase64EncodingOptions())
}
}
public var description: String {
return "<PublicKey: '\(humanReadable)'>"
}
public convenience init(humanReadableString: String) {
let data = NSData(base64EncodedString: humanReadableString, options: NSDataBase64DecodingOptions())!
var array = [UInt8](count: data.length, repeatedValue: 0)
data.getBytes(&array,length:data.length)
self.init(publicKeyBytes: array)
}
}
\ No newline at end of file
......@@ -44,4 +44,8 @@ public final class PublicKey {
self.bytes = publicKeyBytes
self.secretKey = nil
}
}
func == (a: PublicKey, b: PublicKey) -> Bool {
return a.bytes == b.bytes
}
\ No newline at end of file
......@@ -50,5 +50,12 @@ class KeyTests : XCTestCase {
func testCryptoBoxKey() {
let _ = Key(forCryptoBox: true)
}
func testHumanReadable() {
let a = PublicKey(secretKey: Key(forCryptoBox: true))
let str = a.humanReadable
let b = PublicKey(humanReadableString: str)
XCTAssert(a == b)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment