mirror of
https://github.com/inverse-inc/sogo.git
synced 2026-05-24 21:05:25 +00:00
propagate from branch 'ca.inverse.sogo.1_3_15' (head 825d6255230167e6f80d8015a569afa6bdb4cb7d)
to branch 'ca.inverse.sogo' (head 0ed6b9d5a5d4a8143d08cc4652384f5073d26096) Monotone-Parent: 0ed6b9d5a5d4a8143d08cc4652384f5073d26096 Monotone-Parent: 825d6255230167e6f80d8015a569afa6bdb4cb7d Monotone-Revision: c32cff29d1d0d305b798b73f0db514f448a0d44c Monotone-Author: wsourdeau@inverse.ca Monotone-Date: 2012-04-16T15:25:22 Monotone-Branch: ca.inverse.sogo
This commit is contained in:
@@ -1,3 +1,12 @@
|
||||
2012-04-16 Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
||||
|
||||
* SoObjects/SOGo/NSString+Utilities.m (-encryptWithKey:)
|
||||
(-decryptWithKey:): new methods providing symmetric encryption of
|
||||
strings.
|
||||
|
||||
* Tests/Unit/SOGoTestRunner.m (-run): do not empty the pool after
|
||||
each test.
|
||||
|
||||
2012-04-13 Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
||||
|
||||
* SoObjects/Appointments/SOGoAppointmentFolder.m (-bareFetchFields: ...),
|
||||
|
||||
@@ -74,6 +74,11 @@
|
||||
|
||||
- (NSUInteger) countOccurrencesOfString: (NSString *) substring;
|
||||
|
||||
|
||||
/* Those methods provide symmetric enc-/decryption via a XOR operation */
|
||||
- (NSString *) encryptWithKey: (NSString *) theKey;
|
||||
- (NSString *) decryptWithKey: (NSString *) theKey;
|
||||
|
||||
@end
|
||||
|
||||
#endif /* NSSTRING_URL_H */
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -68,11 +68,9 @@
|
||||
NSEnumerator *allTestClasses;
|
||||
NSString *class;
|
||||
SOGoTest *test;
|
||||
NSAutoreleasePool *pool;
|
||||
int rc;
|
||||
|
||||
rc = 0;
|
||||
pool = [NSAutoreleasePool currentPool];
|
||||
|
||||
[self retain];
|
||||
allTestClasses = [[SOGoTest allTestClasses] objectEnumerator];
|
||||
@@ -83,8 +81,7 @@
|
||||
[test setTestRunner: self];
|
||||
if (![test run])
|
||||
rc |= -1;
|
||||
[test autorelease];
|
||||
[pool emptyPool];
|
||||
[test release];
|
||||
}
|
||||
|
||||
[self displayReport];
|
||||
|
||||
@@ -47,4 +47,27 @@
|
||||
failIf(count != 0);
|
||||
}
|
||||
|
||||
- (void) test_encryptdecrypt
|
||||
{
|
||||
NSString *secret = @"this is a secret";
|
||||
NSString *password = @"qwerty";
|
||||
NSString *encresult, *decresult;
|
||||
|
||||
encresult = [secret encryptWithKey: nil];
|
||||
failIf(encresult != nil);
|
||||
encresult = [secret encryptWithKey: @""];
|
||||
failIf(encresult != nil);
|
||||
|
||||
encresult = [secret encryptWithKey: password];
|
||||
failIf(encresult == nil);
|
||||
|
||||
decresult = [encresult decryptWithKey: nil];
|
||||
failIf(decresult != nil);
|
||||
decresult = [encresult decryptWithKey: @""];
|
||||
failIf(decresult != nil);
|
||||
|
||||
decresult = [encresult decryptWithKey: password];
|
||||
failIf(![decresult isEqualToString: secret]);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user