mirror of
https://github.com/inverse-inc/sogo.git
synced 2026-07-05 16:35:10 +00:00
Monotone-Parent: 2fb9aaf01f9b4a29e3d49d6c35deaf48a4e57e9c
Monotone-Revision: 825d6255230167e6f80d8015a569afa6bdb4cb7d Monotone-Author: wsourdeau@inverse.ca Monotone-Date: 2012-04-16T15:24:16
This commit is contained in:
@@ -35,6 +35,7 @@
|
||||
|
||||
#import <NGExtensions/NSDictionary+misc.h>
|
||||
#import <NGExtensions/NSObject+Logs.h>
|
||||
#import <NGExtensions/NGBase64Coding.h>
|
||||
|
||||
#import <NGMime/NGMimeHeaderFieldGenerator.h>
|
||||
#import <SBJson/SBJsonParser.h>
|
||||
@@ -610,4 +611,85 @@ static int cssEscapingCount;
|
||||
return count;
|
||||
}
|
||||
|
||||
- (NSString *) encryptWithKey: (NSString *) theKey
|
||||
{
|
||||
NSMutableData *encryptedPassword;
|
||||
NSMutableString *key;
|
||||
NSString *result;
|
||||
NSUInteger i, passLength, theKeyLength, keyLength;
|
||||
unichar p, k, e;
|
||||
|
||||
if ([theKey length] > 0)
|
||||
{
|
||||
// The length of the key must be greater (or equal) than
|
||||
// the length of the password
|
||||
key = [NSMutableString string];
|
||||
keyLength = 0;
|
||||
|
||||
passLength = [self length];
|
||||
theKeyLength = [theKey length];
|
||||
while (keyLength < passLength)
|
||||
{
|
||||
[key appendString: theKey];
|
||||
keyLength += theKeyLength;
|
||||
}
|
||||
|
||||
encryptedPassword = [NSMutableData data];
|
||||
for (i = 0; i < passLength; i++)
|
||||
{
|
||||
p = [self characterAtIndex: i];
|
||||
k = [key characterAtIndex: i];
|
||||
e = p ^ k;
|
||||
[encryptedPassword appendBytes: (void *)&e length: 2];
|
||||
}
|
||||
|
||||
result = [encryptedPassword stringByEncodingBase64];
|
||||
}
|
||||
else
|
||||
result = nil;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
- (NSString *) decryptWithKey: (NSString *) theKey
|
||||
{
|
||||
NSMutableString *result;
|
||||
NSMutableString *key;
|
||||
NSData *decoded;
|
||||
unichar *decryptedPassword;
|
||||
NSUInteger i, theKeyLength, keyLength, decodedLength;
|
||||
unichar p, k;
|
||||
|
||||
if ([theKey length] > 0)
|
||||
{
|
||||
decoded = [self dataByDecodingBase64];
|
||||
decryptedPassword = (unichar *)[decoded bytes];
|
||||
|
||||
// The length of the key must be greater (or equal) than
|
||||
// the length of the password
|
||||
key = [NSMutableString string];
|
||||
keyLength = 0;
|
||||
decodedLength = ([decoded length] / 2); /* 1 unichar = 2 bytes/char */
|
||||
theKeyLength = [theKey length];
|
||||
|
||||
while (keyLength < decodedLength)
|
||||
{
|
||||
[key appendString: theKey];
|
||||
keyLength += theKeyLength;
|
||||
}
|
||||
|
||||
result = [NSMutableString string];
|
||||
for (i = 0; i < decodedLength; i++)
|
||||
{
|
||||
k = [key characterAtIndex: i];
|
||||
p = decryptedPassword[i] ^ k;
|
||||
[result appendFormat: @"%C", p];
|
||||
}
|
||||
}
|
||||
else
|
||||
result = nil;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user