home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / DEV.Z / Authenticator.m < prev    next >
Encoding:
Text File  |  1996-02-05  |  1.6 KB  |  51 lines

  1. #import "Authenticator.h"
  2.  
  3. unsigned char byteCheckSum(unsigned char input, const unsigned char *bytes, unsigned limit) {
  4.     unsigned index;
  5.     unsigned char result = input;
  6.  
  7.     for (index = 0; index < limit; ++index)
  8.         result ^= bytes[index];
  9.     return result;
  10. }
  11.  
  12.  
  13. @implementation Authenticator
  14.  
  15. // we will offer our class as the signature generator, thus we use
  16. // factory methods as the delegates to NSConnection
  17. + (NSData *)authenticationDataForComponents:(NSArray *)components {
  18.     unsigned int index;
  19.     unsigned char checksum = 0;
  20.  
  21.     for (index = 0; index < [components count]; ++index) {
  22.         id item = [components objectAtIndex:index];
  23.  
  24.         if ([item isKindOfClass:[NSData class]])
  25.             checksum = byteCheckSum(checksum, [item bytes], [item length]);
  26.     }
  27.  
  28.     return [NSData dataWithBytes:&checksum length:1];
  29. }
  30.  
  31. + (BOOL)authenticateComponents:(NSArray *)components withData:(NSData *)signature {
  32.     //  verify authentication data
  33.     // A real authenticator would have a way of verifying the signature without
  34.     // recomputing it.  We don't, in this example, so just recompute.
  35.     NSData *recomputedSignature = [self authenticationDataForComponents:components];
  36.     
  37.     if (![recomputedSignature isEqual:signature]) {
  38.         NSLog(@"received signature %@ doesn't match computed signature %@", signature, recomputedSignature);
  39.         return NO;
  40.     }
  41.     return YES;
  42. }
  43.  
  44. + (BOOL)connection:(NSConnection *)ancestor shouldMakeNewConnection:(NSConnection *)conn {
  45.     // set up all new (per-client) connections to have the same delegate
  46.     [conn setDelegate:[ancestor delegate]];
  47.     return YES;
  48. }
  49.  
  50.  
  51. @end