-
Notifications
You must be signed in to change notification settings - Fork 13
/
PassEntry.mm
59 lines (48 loc) · 1.58 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
58
/*
* Copyright (C) 2012 Brian A. Mattern <rephorm@rephorm.com>.
* All Rights Reserved.
* This file is licensed under the GPLv2+.
* Please see COPYING for more information
*/
#import "PassEntry.h"
#import <Foundation/NSTask.h>
@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
{
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];
[task release];
if (status == 0) {
NSFileHandle *ofile = [opipe fileHandleForReading];
NSData *data = [ofile readDataToEndOfFile];
NSString *str= [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// return first line of file
return [[str componentsSeparatedByString:@"\n"] objectAtIndex:0];
} 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