Signal-iOS/SignalServiceKit/Protocols/ProtoUtils.m
Ehren Kret 557d6a67cc migrate Cryptography over to swift
`srand` is not accessible in swift. Remove `srand` anyhow as anything
using `rand` for any actual randomness should be considered a bug in
need of fixing rather than viewing `srand` as an acceptable workaround.
2024-08-01 14:43:34 -05:00

88 lines
2.5 KiB
Objective-C

//
// Copyright 2018 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
#import "ProtoUtils.h"
#import "ProfileManagerProtocol.h"
#import "TSThread.h"
#import <SignalServiceKit/SignalServiceKit-Swift.h>
NS_ASSUME_NONNULL_BEGIN
@implementation ProtoUtils
+ (OWSAES256Key *)localProfileKey
{
return self.profileManagerObjC.localProfileKey;
}
#pragma mark -
+ (BOOL)shouldMessageHaveLocalProfileKey:(TSThread *)thread transaction:(SDSAnyReadTransaction *)transaction
{
OWSAssertDebug(thread);
OWSAssertDebug(transaction);
// Group threads will return YES if the group is in the whitelist
// Contact threads will return YES if the contact is in the whitelist.
return [self.profileManagerObjC isThreadInProfileWhitelist:thread transaction:transaction];
}
+ (void)addLocalProfileKeyIfNecessary:(TSThread *)thread
dataMessageBuilder:(SSKProtoDataMessageBuilder *)dataMessageBuilder
transaction:(SDSAnyReadTransaction *)transaction
{
OWSAssertDebug(thread);
OWSAssertDebug(dataMessageBuilder);
OWSAssertDebug(transaction);
if ([self shouldMessageHaveLocalProfileKey:thread transaction:transaction]) {
[dataMessageBuilder setProfileKey:self.localProfileKey.keyData];
}
}
+ (void)addLocalProfileKeyToDataMessageBuilder:(SSKProtoDataMessageBuilder *)dataMessageBuilder
{
OWSAssertDebug(dataMessageBuilder);
[dataMessageBuilder setProfileKey:self.localProfileKey.keyData];
}
+ (void)addLocalProfileKeyIfNecessary:(TSThread *)thread
callMessageBuilder:(SSKProtoCallMessageBuilder *)callMessageBuilder
transaction:(SDSAnyReadTransaction *)transaction
{
OWSAssertDebug(thread);
OWSAssertDebug(callMessageBuilder);
OWSAssertDebug(transaction);
if ([self shouldMessageHaveLocalProfileKey:thread transaction:transaction]) {
[callMessageBuilder setProfileKey:self.localProfileKey.keyData];
}
}
+ (nullable NSString *)parseProtoE164:(nullable NSString *)value name:(NSString *)name
{
if (value == nil) {
OWSFailDebug(@"%@ was unexpectedly nil.", name);
return nil;
}
if (value.length == 0) {
OWSFailDebug(@"%@ was unexpectedly empty.", name);
return nil;
}
if (!value.isStructurallyValidE164) {
if (SSKDebugFlags.internalLogging) {
OWSFailDebug(@"%@ was unexpectedly invalid: %@.", name, value);
}
OWSFailDebug(@"%@ was unexpectedly invalid.", name);
return nil;
}
return value;
}
@end
NS_ASSUME_NONNULL_END