2013年4月11日木曜日

Message digests (MD5, SHA1, etc.) on iOS with dedicated classes

Project nv-ios-digest (Apache License, Version 2.0) provides Objective-C classes that are dedicated to computation of message digests such as MD5 and SHA-1. An example code below prints MD5 hash value of "Hello, world.".

// Create an MD5 instance using the convenience constructor.
MD5 *md5 = [MD5 md5WithString:@"Hello, world."];

// Print the MD5 hash value as string.
// This will show "md5 = 080aef839b95facf73ec599375e92d47".
NSLog(@"md5 = %@", md5);


Another example code below shows another different way to compute MD5 with update and final.

// Create MD5 instance for computation.
md5 = [[MD5 alloc] init];

// Update. See also
// updateWith:(const void *)data length:(CC_LONG)length
[md5 updateWithString:@"Hello, world."];

// Final. The returned pointer points to the internal buffer
// of md5 whose size is CC_MD5_DIGEST_LENGTH (=16).
unsigned char *md = [md5 final];

// After 'final', a valid NSString expression is available
// through 'description' method.
NSLog(@"md5 = %@", md5);


Supported algorithms are as follows.

  • MD5
  • SHA1
  • SHA224
  • SHA256
  • SHA384
  • SHA512

A simple implementation to add md5 method to NSString will look like the following.

// NSString+MessageDigest.h
#import <Foundation/Foundation.h>
#import "MD5.h"
@interface NSString (MessageDigest)
- (MD5 *)md5;
@end

// NSString+MessageDigest.m
#import "NSString+MessageDigest.h"
@implementation NSString (MessageDigest)
- (MD5 *)md5
{
    return [MD5 md5WithString:self];
}
@end

And its usage will be:

#import "NSString+MessageDigest.h"

NSLog(@"md5 = %@", @"Hello, world.".md5);


Project Page
https://github.com/TakahikoKawasaki/nv-ios-digest


2013年4月3日水曜日

Get and compare iOS version at runtime with Version class

With Version class that is contained in nv-ios-version project (Apache License, Version 2.0), it is easy to get and compare iOS version. An example code below dumps the iOS version and check whether the version is greater than or equal to 6.0.

// Get the system version of iOS at runtime.
NSString *versionString = [[UIDevice currentDevice] systemVersion];

// Convert the version string to a Version instance.
Version *version = [Version versionWithString:versionString];

// Dump the major, minor and micro version numbers.
NSLog(@"version = [%d, %d, %d]",
    version.major, version.minor, version.micro);

// Check whether the version is greater than or equal to 6.0.
if ([version isGreaterThanOrEqualToMajor:6 minor:0])
{
    // The iOS version is greater than or equal to 6.0.
}

// Another way to check whether iOS version is
// greater than or equal to 6.0
if (6 <= version.major)
{
    // The iOS version is greater than or equal to 6.0.
}

In Version API, the valid range of major, minor and micro is between 0 and 255. If an invalid value is given, NSInvalidArgumentException is thrown. The default values of major, minor and micro are 0.

number in the API is "(major << 16) | (minor << 8) | micro".

description method returns "major.minor" when micro is 0. Otherwise, "major.minor.micro". For example, "6.1" is returned for version 6.1.0, and "6.1.1" for version 6.1.1.

Version class provides many comparison methods.

#pragma mark -
#pragma mark Comparison
- (NSComparisonResult)compareToMajor:(int)major;
- (NSComparisonResult)compareToMajor:(int)major minor:(int)minor;
- (NSComparisonResult)compareToMajor:(int)major minor:(int)minor micro:(int)micro;
- (NSComparisonResult)compareToNumber:(int)number;
- (NSComparisonResult)compareTo:(Version *)aVersion;

#pragma mark -
#pragma mark Comparison (equal to)
- (BOOL)isEqualToMajor:(int)major;
- (BOOL)isEqualToMajor:(int)major minor:(int)minor;
- (BOOL)isEqualToMajor:(int)major minor:(int)minor micro:(int)micro;
- (BOOL)isEqualToNumber:(int)number;
- (BOOL)isEqualTo:(Version *)aVersion;

#pragma mark -
#pragma mark Comparison (not equal to)
- (BOOL)isNotEqualToMajor:(int)major;
- (BOOL)isNotEqualToMajor:(int)major minor:(int)minor;
- (BOOL)isNotEqualToMajor:(int)major minor:(int)minor micro:(int)micro;
- (BOOL)isNotEqualToNumber:(int)number;
- (BOOL)isNotEqualTo:(Version *)aVersion;

#pragma mark -
#pragma mark Comparison (less than)
- (BOOL)isLessThanMajor:(int)major;
- (BOOL)isLessThanMajor:(int)major minor:(int)minor;
- (BOOL)isLessThanMajor:(int)major minor:(int)minor micro:(int)micro;
- (BOOL)isLessThanNumber:(int)number;
- (BOOL)isLessThan:(Version *)aVersion;

#pragma mark -
#pragma mark Comparison (less than or equal to)
- (BOOL)isLessThanOrEqualToMajor:(int)major;
- (BOOL)isLessThanOrEqualToMajor:(int)major minor:(int)minor;
- (BOOL)isLessThanOrEqualToMajor:(int)major minor:(int)minor micro:(int)micro;
- (BOOL)isLessThanOrEqualToNumber:(int)number;
- (BOOL)isLessThanOrEqualTo:(Version *)aVersion;

#pragma mark -
#pragma mark Comparison (greater than)
- (BOOL)isGreaterThanMajor:(int)major;
- (BOOL)isGreaterThanMajor:(int)major minor:(int)minor;
- (BOOL)isGreaterThanMajor:(int)major minor:(int)minor micro:(int)micro;
- (BOOL)isGreaterThanNumber:(int)number;
- (BOOL)isGreaterThan:(Version *)aVersion;

#pragma mark -
#pragma mark Comparison (greater than or equal to)
- (BOOL)isGreaterThanOrEqualToMajor:(int)major;
- (BOOL)isGreaterThanOrEqualToMajor:(int)major minor:(int)minor;
- (BOOL)isGreaterThanOrEqualToMajor:(int)major minor:(int)minor micro:(int)micro;
- (BOOL)isGreaterThanOrEqualToNumber:(int)number;
- (BOOL)isGreaterThanOrEqualTo:(Version *)aVersion;

Convenience constructors are listed below.

#pragma mark -
#pragma mark Convenience Constructors
+ (Version *)versionWithMajor:(int)major;
+ (Version *)versionWithMajor:(int)major minor:(int)minor;
+ (Version *)versionWithMajor:(int)major minor:(int)minor micro:(int)micro;
+ (Version *)versionWithString:(NSString *)string;


The following sample code shows how to implement System class that provides version method which returns a Version instance for iOS version.

// System.h
#import <Foundation/Foundation.h>
#import "Version.h"

@interface System : NSObject
/**
 * Get iOS version.
 */
+ (Version *)version;
@end

// System.m
#import <UIKit/UIDevice.h>
#import "System.h"

static Version *_version;

@implementation System
/**
 * Initialization for this class.
 */ 
+ (void)initialize
{
    // Get the system version of iOS at runtime.
    NSString *versionString = [[UIDevice currentDevice] systemVersion];

    // Convert the version string to a Version instance.
    Version *version = [Version versionWithString:versionString];

    // Set as a class variable.
    _version = version;
}

/**
 * Get iOS version.
 */
+ (Version *)version
{
    return _version;
}
@end

With System class implemented like above, code to check iOS version becomes shorter.

if (System.version.major >= 6)
{
    // iOS version is greater than or equal to 6.0.
}


Project Page
https://github.com/TakahikoKawasaki/nv-ios-version