mirror of
https://github.com/signalapp/libsignal.git
synced 2026-04-25 17:25:18 +02:00
...to have a period after "Signal Messenger, LLC." ...except for the Java sources, which still need a cleanup pass.
89 lines
2.9 KiB
Swift
89 lines
2.9 KiB
Swift
//
|
|
// Copyright 2020-2021 Signal Messenger, LLC.
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
//
|
|
|
|
import SignalFfi
|
|
import Foundation
|
|
|
|
public class SignalMessage: NativeHandleOwner {
|
|
internal override class func destroyNativeHandle(_ handle: OpaquePointer) -> SignalFfiErrorRef? {
|
|
return signal_message_destroy(handle)
|
|
}
|
|
|
|
public convenience init<Bytes: ContiguousBytes>(bytes: Bytes) throws {
|
|
var result: OpaquePointer?
|
|
try bytes.withUnsafeBytes {
|
|
try checkError(signal_message_deserialize(&result, $0.baseAddress?.assumingMemoryBound(to: UInt8.self), $0.count))
|
|
}
|
|
self.init(owned: result!)
|
|
}
|
|
|
|
public var senderRatchetKey: PublicKey {
|
|
return withNativeHandle { nativeHandle in
|
|
failOnError {
|
|
try invokeFnReturningNativeHandle {
|
|
signal_message_get_sender_ratchet_key($0, nativeHandle)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public var body: [UInt8] {
|
|
return withNativeHandle { nativeHandle in
|
|
failOnError {
|
|
try invokeFnReturningArray {
|
|
signal_message_get_body($0, $1, nativeHandle)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public func serialize() -> [UInt8] {
|
|
return withNativeHandle { nativeHandle in
|
|
failOnError {
|
|
try invokeFnReturningArray {
|
|
signal_message_get_serialized($0, $1, nativeHandle)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public var messageVersion: UInt32 {
|
|
return withNativeHandle { nativeHandle in
|
|
failOnError {
|
|
try invokeFnReturningInteger {
|
|
signal_message_get_message_version($0, nativeHandle)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public var counter: UInt32 {
|
|
return withNativeHandle { nativeHandle in
|
|
failOnError {
|
|
try invokeFnReturningInteger {
|
|
signal_message_get_counter($0, nativeHandle)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public func verifyMac<Bytes: ContiguousBytes>(sender: PublicKey,
|
|
receiver: PublicKey,
|
|
macKey: Bytes) throws -> Bool {
|
|
return try withNativeHandles(self, sender, receiver) { messageHandle, senderHandle, receiverHandle in
|
|
try macKey.withUnsafeBytes {
|
|
var result: Bool = false
|
|
try checkError(signal_message_verify_mac(&result,
|
|
messageHandle,
|
|
senderHandle,
|
|
receiverHandle,
|
|
$0.baseAddress?.assumingMemoryBound(to: UInt8.self),
|
|
$0.count))
|
|
return result
|
|
}
|
|
}
|
|
}
|
|
}
|