-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXMFabDocument+PublicKeyObfuscation.m
110 lines (78 loc) · 3.48 KB
/
XMFabDocument+PublicKeyObfuscation.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// CodexFab
//
// XMFabDocument (PublicKeyObfuscation)
//
// Licensed under CC Attribution License 3.0 <http://creativecommons.org/licenses/by/3.0/>
//
// Based on CocoaFob by Gleb Dolgich
// <http://github.com/gbd/cocoafob/tree/master>
//
// Created by Mantis on 10/07/09.
// Copyright 2009 MachineCodex Software Australia. All rights reserved.
// <http://www.machinecodex.com>
#import "XMFabDocument.h"
//TODO: Make sure that \n newline characters are not broken across lines.trin
@implementation XMFabDocument (PublicKeyObfuscation)
- (IBAction) showObfusactorPanel:(id)sender {
[self generateObfuscatedCode:self];
[NSApp beginSheet:obfuscatorPanel modalForWindow:[self windowForSheet] modalDelegate:self didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:nil];
}
- (IBAction) closeObfusactorPanel:(id)sender {
[obfuscatorPanel orderOut:self];
[NSApp endSheet:obfuscatorPanel];
}
- (void) sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo {
[self closeObfusactorPanel:self];
}
- (void)generateObfuscatedCode:(id)sender {
NSString *pubKey = [[self pem] valueForKey:@"publicKey"];
if(pubKey)
self.obfuscatedPublicKey = [self obfuscatedCocoaCodeForPublicKey:pubKey];
}
- (NSString *)obfuscatedCocoaCodeForPublicKey:(NSString*)publicKeyString {
//Generates a block of Cocoa code that assembles the public key
//from variable-length substrings into an NSMutableString
// Define some Cocoa-code-wrapper strings for substitution
NSString *openmsg = @"NSMutableString *pkb64 = [NSMutableString string];\n";
NSString *closeMsg = @"NSString *publicKey = [NSString stringWithString:pkb64];\n";
NSString *pkb64FragOpen = @"[pkb64 appendString:@\"";
NSString *pkb64FragClose = @"\"];\n";
//Container for the Cocoa code-as-text
NSMutableString *pkb64Fragmented = [NSMutableString string];
//Container for the publicKeyString
NSMutableString *tempKey = [NSMutableString string];
//remove first & last lines
NSArray *lines = [publicKeyString componentsSeparatedByString:@"\n"];
int i, cnt = [lines count];
for (i=1; i < cnt -2; i++) {
[tempKey appendString:[NSString stringWithFormat:@"%@\n",[lines objectAtIndex:i]]];
}
// escape newlines (so they will display as textual characters)
[tempKey replaceOccurrencesOfString:@"\n"
withString:@"\\n"
options:NSLiteralSearch
range:NSMakeRange(0, [tempKey length])];
int length = [tempKey length];
// define min & max substring lengths - these could be exposed in the gui
int maxStringLength = (length/8);
int minStringLength = 3;
// init rand()
srand(time(NULL));
// break up the string into random length chunks
int firstIndex = 0;
while (firstIndex < length) {
// walk through the key from start to end, biting off random-length
// chunks and appending them by format to the Cocoa-code-string
int tlength = ( rand() % maxStringLength ) + minStringLength;
if(firstIndex + tlength > length) tlength = length - firstIndex;
NSString *line = [tempKey substringWithRange:NSMakeRange(firstIndex, tlength)];
if ([line hasSuffix:@"\\"]) { //Make sure our line doesn't end in a backslash as this may split a newline token
tlength += 2;
line = [tempKey substringWithRange:NSMakeRange(firstIndex, tlength)];
}
[pkb64Fragmented appendString:[NSString stringWithFormat:@"%@%@%@",pkb64FragOpen,line,pkb64FragClose]];
firstIndex += tlength;
}
return [NSString stringWithFormat:@"%@%@%@",openmsg,pkb64Fragmented,closeMsg];
}
@end