Skip to content

Commit

Permalink
BREAKING: Convert prehashPassword from exception to errors
Browse files Browse the repository at this point in the history
Required for compat with Swift, which doesn't have exceptions
  • Loading branch information
tux3 committed Apr 16, 2024
1 parent 283ee30 commit 32c3552
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Tanker/Headers/TKRTanker.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

+ (nonnull NSString*)nativeVersionString;

+ (nonnull NSString*)prehashPassword:(nonnull NSString*)password;
+ (nullable NSString*)prehashPassword:(nonnull NSString*)password err:(NSError**)errResult;

+ (void)connectLogHandler:(nonnull TKRLogHandler)handler;

Expand Down
9 changes: 6 additions & 3 deletions Tanker/Sources/TKRTanker.m
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,13 @@ + (nonnull NSString*)nativeVersionString
return [NSString stringWithCString:tanker_version_string() encoding:NSUTF8StringEncoding];
}

+ (nonnull NSString*)prehashPassword:(nonnull NSString*)password
+ (nullable NSString*)prehashPassword:(nonnull NSString*)password err:(NSError**)errResult
{
if (!password.length)
[NSException raise:NSInvalidArgumentException format:@"cannot hash empty password"];
if (!password.length) {
if (errResult != nil)
*errResult = TKR_createNSError(TKRErrorInvalidArgument, @"cannot hash empty password");
return nil;
}

char const* c_password = [password cStringUsingEncoding:NSUTF8StringEncoding];
tanker_expected_t* expected_chashed = tanker_prehash_password(c_password);
Expand Down
11 changes: 7 additions & 4 deletions Tanker/Tests/UnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ class UnitTests: QuickSpec {
describe("prehashPassword") {
it("should fail to hash an empty password") {
expect {
TKRTanker.prehashPassword("")
}.to(raiseException(named: "NSInvalidArgumentException"))
try TKRTanker.prehashPassword("")
}.to(throwError { (error: NSError) in
expect(error.domain).to(equal(TKRErrorDomain));
expect(error.code) == TKRError.invalidArgument.rawValue;
})
}

it("should hash a test vector 1") {
let hashed = TKRTanker.prehashPassword("super secretive password");
let hashed = try! TKRTanker.prehashPassword("super secretive password");
let expected = "UYNRgDLSClFWKsJ7dl9uPJjhpIoEzadksv/Mf44gSHI=";
expect(hashed) == expected;
}

it("should hash a test vector 2") {
let hashed = TKRTanker.prehashPassword("test éå 한국어 😃");
let hashed = try! TKRTanker.prehashPassword("test éå 한국어 😃");
let expected = "Pkn/pjub2uwkBDpt2HUieWOXP5xLn0Zlen16ID4C7jI=";
expect(hashed) == expected;
}
Expand Down

0 comments on commit 32c3552

Please sign in to comment.