Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 110 lines (89 sloc) 3.205 kb
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
//
// NVPasswordGenerator.h
// Notation
//
// Created by Brian Bergstrand on 9/27/2009.
// Copyright 2009 Brian Bergstrand. All rights reserved.
//

#include "NVPasswordGenerator.h"

#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
#include <dispatch/dispatch.h>
#endif

static const char nvDecimalSet[] = "0123456789";
static const char nvLowerCaseSet[] = "abcdefghijklmnopqrstuvwxyz";
static const char nvUpperCaseSet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static const char nvSymbolSet[] = "!@#$%^&*()-+=?/<>";

@implementation NVPasswordGenerator

+ (NSString*)passwordWithOptions:(NVPasswordOptions)options length:(NSUInteger)len
{
    char source[1024];
    source[0] = 0;
    
    if (0 != (options & knvPasswordNumeric))
        (void)strlcat(source, nvDecimalSet, sizeof(source));
    if (0 != (options & knvPasswordAlpha))
        (void)strlcat(source, nvLowerCaseSet, sizeof(source));
    if (0 != (options & knvPasswordMixedCase))
        (void)strlcat(source, nvUpperCaseSet, sizeof(source));
    if (0 != (options & knvPasswordSymbol))
        (void)strlcat(source, nvSymbolSet, sizeof(source));
    
    // Fill the buffer
    size_t i = strlen(source);
    size_t srclen = sizeof(source)-1 - i;
    (void)strlcpy(&source[i], source, srclen);
    
    char gen[len+1];
    gen[0] = 0;
    srclen = strlen(source);
    for (i = 0; i < len; ++i) {
        char c;
        do {
            c = source[arc4random() % srclen];
        } while (0 == (options & knvPasswordDuplicates) && NULL != strchr(gen, c));
        
        gen[i] = c;
        gen[i+1] = 0;
    }

    return ([NSString stringWithCString:gen encoding:NSASCIIStringEncoding]);
}

+ (NSString*)numericPasswordWithLength:(NSUInteger)len
{
    return ([self passwordWithOptions:knvPasswordNumeric length:len]);
}

+ (NSString*)alphaNumericPasswordWithLength:(NSUInteger)len
{
    return ([self passwordWithOptions:knvPasswordNumeric|knvPasswordAlpha|knvPasswordMixedCase length:len]);
}

+ (NSString*)lightNumeric
{
    return ([self passwordWithOptions:knvPasswordNumeric length:6]);
}

+ (NSString*)light
{
    return ([self passwordWithOptions:knvPasswordNumeric|knvPasswordAlpha|knvPasswordDuplicates length:6]);
}

+ (NSString*)medium
{
    return ([self passwordWithOptions:knvPasswordNumeric|knvPasswordAlpha|knvPasswordMixedCase length:8]);
}

+ (NSString*)strong
{
    return ([self passwordWithOptions:knvPasswordNumeric|knvPasswordAlpha|knvPasswordMixedCase|knvPasswordSymbol length:10]);
}

+ (NSArray*)suggestions
{
    enum {numSuggestions = 4};
    const SEL methods[numSuggestions] = {@selector(strong), @selector(medium), @selector(light), @selector(lightNumeric)};
    
    NSString* suggest[numSuggestions];
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
    dispatch_apply(numSuggestions, dispatch_get_global_queue(0, 0), ^(size_t i) {
#else
    size_t i;
    for (i = 0; i < numSuggestions; ++i) {
#endif
        suggest[i] = [self performSelector:methods[i]];
    }
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
    );
#endif
    
    NSArray *a = [NSArray arrayWithObjects:suggest[0], suggest[1], suggest[2], suggest[3], nil];
    return (a);
}

@end
Something went wrong with that request. Please try again.