This repository has been archived by the owner on Sep 1, 2020. It is now read-only.
forked from myeyesareblind/pass-ios
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPassEntry.mm
58 lines (48 loc) · 1.73 KB
/
PassEntry.mm
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
/*
* Copyright (C) 2012 Brian A. Mattern <[email protected]>.
* Copyright (C) 2015 David Beitey <[email protected]>.
* All Rights Reserved.
* This file is licensed under the GPLv2+.
* Please see COPYING for more information
*/
#import "PassEntry.h"
#import "NSTask.h" // Not documented on iOS in Foundation but still available
@implementation PassEntry
@synthesize name, path, is_dir, pass;
- (NSString *)name
{
if ([name hasSuffix:@".gpg"])
return [name substringToIndex:[name length] - 4];
else
return name;
}
- (NSString *)passWithPassphrase:(NSString *)passphrase passwordOnly:(BOOL)passwordOnly
{
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/gpg"];
[task setArguments:[NSArray arrayWithObjects:
@"--passphrase",passphrase,@"-d",@"--batch",
@"--quiet",@"--no-tty",self.path,nil]];
NSPipe *opipe = [NSPipe pipe];
[task setStandardOutput:opipe];
NSPipe *erroPipe = [NSPipe pipe];
[task setStandardError:erroPipe];
[task launch];
[task waitUntilExit];
int status = [task terminationStatus];
if (status == 0) {
NSFileHandle *ofile = [opipe fileHandleForReading];
NSData *data = [ofile readDataToEndOfFile];
NSString *str= [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// return first line of file or all of file
return passwordOnly ? [[str componentsSeparatedByString:@"\n"] objectAtIndex:0] : str;
} else {
NSFileHandle *ofile = [erroPipe fileHandleForReading];
NSData *data = [ofile readDataToEndOfFile];
NSString *str= [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"error string: %@", str);
// XXX handle error
return nil;
}
}
@end