forked from arqbackup/arq_restore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArqSalt.m
75 lines (69 loc) · 2.47 KB
/
ArqSalt.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// ArqSalt.m
// Arq
//
// Created by Stefan Reitshamer on 7/16/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "ArqSalt.h"
#import "S3AuthorizationProvider.h"
#import "S3Service.h"
#import "Blob.h"
#import "BlobACL.h"
#import "NSFileManager_extra.h"
#import "UserLibrary_Arq.h"
#define SALT_LENGTH (8)
@interface ArqSalt (internal)
- (NSData *)createRandomSalt;
@end
@implementation ArqSalt
- (id)initWithAccessKeyID:(NSString *)theAccessKeyID
secretAccessKey:(NSString *)theSecretAccessKey
s3BucketName:(NSString *)theS3BucketName
computerUUID:(NSString *)theComputerUUID {
if (self = [super init]) {
accessKeyID = [theAccessKeyID retain];
secretAccessKey = [theSecretAccessKey retain];
s3BucketName = [theS3BucketName retain];
computerUUID = [theComputerUUID retain];
localPath = [[NSString alloc] initWithFormat:@"%@/Cache.noindex/%@/%@/salt.dat", [UserLibrary arqUserLibraryPath], s3BucketName, computerUUID];
s3Path = [[NSString alloc] initWithFormat:@"/%@/%@/salt", s3BucketName, computerUUID];
}
return self;
}
- (void)dealloc {
[accessKeyID release];
[secretAccessKey release];
[s3BucketName release];
[computerUUID release];
[localPath release];
[s3Path release];
[super dealloc];
}
- (NSData *)salt:(NSError **)error {
NSData *ret = [NSData dataWithContentsOfFile:localPath options:NSUncachedRead error:error];
if (ret == nil) {
S3AuthorizationProvider *sap = [[[S3AuthorizationProvider alloc] initWithAccessKey:accessKeyID secretKey:secretAccessKey] autorelease];
S3Service *s3 = [[[S3Service alloc] initWithS3AuthorizationProvider:sap useSSL:YES retryOnTransientError:NO] autorelease];
ret = [s3 dataAtPath:s3Path error:error];
if (ret == nil) {
return nil;
}
NSError *myError = nil;
if (![[NSFileManager defaultManager] ensureParentPathExistsForPath:localPath error:&myError]
|| ![ret writeToFile:localPath options:NSAtomicWrite error:&myError]) {
HSLogError(@"error caching salt data to %@: %@", localPath, myError);
}
}
return ret;
}
@end
@implementation ArqSalt (internal)
- (NSData *)createRandomSalt {
unsigned char buf[SALT_LENGTH];
for (NSUInteger i = 0; i < SALT_LENGTH; i++) {
buf[i] = (unsigned char)(rand() % 256);
}
return [[[NSData alloc] initWithBytes:buf length:SALT_LENGTH] autorelease];
}
@end