-
-
Save vcsjones/8b17709cdd23d7e86cd2f65cd8591723 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
import CryptoKit | |
// ANSI X9.63 formatted, 0x04 used to indicate uncompressed. | |
let publicKeyData = Data([0x04, 0x77, 0xd2, 0x8f, 0x10, 0x3c, 0x37, 0xeb, 0x03, 0xd6, 0x2d, 0xec, | |
0xd4, 0xfd, 0xbd, 0xa0, 0x1d, 0xd6, 0x9f, 0xea, 0x87, 0x83, 0x25, 0xbc, | |
0x1b, 0xeb, 0xc5, 0x07, 0x4a, 0x87, 0x64, 0x55, 0xeb, 0x9d, 0x2f, 0x4e, | |
0x71, 0x9b, 0xa0, 0xa0, 0x83, 0x8d, 0xf3, 0xb0, 0x74, 0x79, 0xed, 0x21, | |
0x79, 0x35, 0x8f, 0x71, 0x1f, 0xe9, 0xd0, 0x04, 0xb6, 0x93, 0xc6, 0x29, | |
0x22, 0xe9, 0x57, 0x72, 0xd0]) | |
let signatureData = Data([0x30, 0x44, 0x02, 0x20, 0x61, 0x51, 0x6A, 0x23, 0xF9, 0x99, 0x62, 0xB4, | |
0x41, 0x7C, 0x87, 0xD2, 0x65, 0x92, 0xB5, 0x20, 0x60, 0xB1, 0x9C, 0x5B, | |
0x74, 0x81, 0xA2, 0x31, 0x8C, 0x66, 0x5A, 0xF5, 0x40, 0xAE, 0x72, 0x1C, | |
0x02, 0x20, 0x69, 0x61, 0x1C, 0x07, 0x3C, 0x6E, 0x34, 0xA3, 0x58, 0x34, | |
0x3E, 0x9A, 0xD4, 0x3B, 0x49, 0x66, 0xCE, 0x0F, 0x9A, 0x89, 0x14, 0xC5, | |
0xE7, 0x7F, 0x2C, 0xB3, 0xFE, 0x28, 0xAE, 0x2B, 0xF4, 0xD0]) | |
let messageData = Data([0x38, 0x90, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbd, 0x3b, 0xde, 0x89, 0x4f, 0x6d, 0xdb, 0x7e, 0x5a, 0xe8, 0x9e, 0x7d, 0x7c, 0x1e, 0xef, 0x5c, 0x27, 0x18, 0x75, 0xc5, 0xa2, 0xbd, 0x6b, 0x75, 0xfa, 0xba, 0x07, 0xc8, 0xc8, 0x42, 0x39, 0x02, 0xe8, 0xc4, 0xd7, 0xa4, 0xeb, 0xf3, 0x54, 0x2d, 0x7d, 0x8e, 0xee, 0x65, 0xa9, 0x96, 0xbb, 0x45, 0x21, 0x1e, 0x54, 0x6b, 0xd9, 0x95, 0xae, 0x5b, 0x1d, 0x09, 0x9a, 0x90, 0x46, 0x48, 0x86, 0x98, 0x1b, 0x9e, 0x47, 0x99, 0x71, 0x01, 0x00, 0x00, 0x21, 0x4e, 0x00, 0x00, 0xbf, 0x17, 0x40, 0xf6, 0xc1, 0xe1, 0xf9, 0x13, 0xd2, 0xf3, 0x07, 0xb2, 0x9b, 0x80, 0x82, 0x55, 0x1b, 0xa9, 0xa5, 0xc5]) | |
let publicKey = try! P256.Signing.PublicKey(x963Representation: publicKeyData) | |
let signature = try! P256.Signing.ECDSASignature.init(derRepresentation: signatureData) | |
let start = DispatchTime.now() | |
let result = publicKey.isValidSignature(signature, for:messageData) | |
let end = DispatchTime.now() | |
let diff = Double((end.uptimeNanoseconds - start.uptimeNanoseconds)) as Double / 1000 / 1000 | |
if (result) { | |
print("Signature is valid.") | |
} else { | |
print("Signature is not valid.") | |
} | |
print("Execution: \(diff) milliseconds.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
import CryptoKit | |
import SecurityFoundation | |
// ANSI X9.63 formatted, 0x04 used to indicate uncompressed. | |
let publicKey = Data([0x04, 0x77, 0xd2, 0x8f, 0x10, 0x3c, 0x37, 0xeb, 0x03, 0xd6, 0x2d, 0xec, | |
0xd4, 0xfd, 0xbd, 0xa0, 0x1d, 0xd6, 0x9f, 0xea, 0x87, 0x83, 0x25, 0xbc, | |
0x1b, 0xeb, 0xc5, 0x07, 0x4a, 0x87, 0x64, 0x55, 0xeb, 0x9d, 0x2f, 0x4e, | |
0x71, 0x9b, 0xa0, 0xa0, 0x83, 0x8d, 0xf3, 0xb0, 0x74, 0x79, 0xed, 0x21, | |
0x79, 0x35, 0x8f, 0x71, 0x1f, 0xe9, 0xd0, 0x04, 0xb6, 0x93, 0xc6, 0x29, | |
0x22, 0xe9, 0x57, 0x72, 0xd0]) | |
// Signature has been re-formatted as a SEQUENCE(R, S) | |
let signature = Data([0x30, 0x44, 0x02, 0x20, 0x61, 0x51, 0x6A, 0x23, 0xF9, 0x99, 0x62, 0xB4, | |
0x41, 0x7C, 0x87, 0xD2, 0x65, 0x92, 0xB5, 0x20, 0x60, 0xB1, 0x9C, 0x5B, | |
0x74, 0x81, 0xA2, 0x31, 0x8C, 0x66, 0x5A, 0xF5, 0x40, 0xAE, 0x72, 0x1C, | |
0x02, 0x20, 0x69, 0x61, 0x1C, 0x07, 0x3C, 0x6E, 0x34, 0xA3, 0x58, 0x34, | |
0x3E, 0x9A, 0xD4, 0x3B, 0x49, 0x66, 0xCE, 0x0F, 0x9A, 0x89, 0x14, 0xC5, | |
0xE7, 0x7F, 0x2C, 0xB3, 0xFE, 0x28, 0xAE, 0x2B, 0xF4, 0xD0]) | |
// A SHA-2-256 hash of the message. | |
let digest = Data([0x4D, 0xD5, 0xFB, 0xD7, 0x96, 0xB4, 0x37, 0xCF, 0x43, 0x4A, 0xA5, 0x2D, | |
0x30, 0x56, 0x7F, 0x1F, 0xF9, 0x26, 0x43, 0xE7, 0x88, 0x59, 0xCF, 0x6C, | |
0xB6, 0x43, 0x50, 0xFC, 0x2C, 0x23, 0x18, 0x6C]) | |
let attributes = [ | |
kSecAttrKeyType: kSecAttrKeyTypeEC, | |
kSecAttrKeyClass: kSecAttrKeyClassPublic, | |
kSecAttrKeySizeInBits: 256 | |
] as [CFString : Any] | |
let key = SecKeyCreateWithData(publicKey as CFData, attributes as CFDictionary, nil)! | |
let transform = SecVerifyTransformCreate(key, signature as CFData, nil)! | |
// Set to SHA2 family, 256-bits | |
SecTransformSetAttribute(transform, kSecInputIsAttributeName, kSecInputIsDigest, nil) | |
SecTransformSetAttribute(transform, kSecTransformInputAttributeName, digest as CFData, nil) | |
SecTransformSetAttribute(transform, kSecDigestTypeAttribute, kSecDigestSHA2, nil) | |
SecTransformSetAttribute(transform, kSecDigestLengthAttribute, 256 as CFNumber, nil) | |
let start = DispatchTime.now() | |
let response = SecTransformExecute(transform, nil) as! Int32 | |
let end = DispatchTime.now() | |
// Divide by 1000 once to go to microseconds, 1000 again to milliseconds | |
let milliSeconds = Double((end.uptimeNanoseconds - start.uptimeNanoseconds)) as Double / 1000.0 / 1000.0 | |
print("Execution: \(milliSeconds) milliseconds.") | |
if (response == 1) { | |
print("Signature is valid.") | |
} else { | |
print("Signature is not valid.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment