Skip to content

Instantly share code, notes, and snippets.

@adchsm
Created June 14, 2017 13:17
Show Gist options
  • Save adchsm/9febef7b62198c89f0c75a4fc04a7b32 to your computer and use it in GitHub Desktop.
Save adchsm/9febef7b62198c89f0c75a4fc04a7b32 to your computer and use it in GitHub Desktop.
Hash Log
/*
MIT License
Copyright (c) 2017 Bugfender
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#import <CommonCrypto/CommonKeyDerivation.h>
NSMutableString *arrayToHex(const uint8_t *data, size_t len) {
NSMutableString *output = [NSMutableString string];
for (int i = 0; i < len; i++) {
[output appendFormat:@"%02x", data[i]];
}
return output;
}
#define SALT_LENGTH 32
/*!
@function HashLogEntry
@abstract Hash the passed in text using password hashing best practices.
@discussion The format of the returned digest is SALT:DIGEST with the
salt and digest hex encoded.
@result Return NSString with the digest on success or nil on error.
*/
NSString *HashLogEntry(NSString *text) {
uint8_t salt[SALT_LENGTH];
// Create a salt using the cryptographic quality random number generator
int ret = SecRandomCopyBytes(kSecRandomDefault, SALT_LENGTH, salt);
if (ret != 0) {
return nil;
}
NSData *input_data = [text dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *output_data = [NSMutableData dataWithLength:CC_SHA512_DIGEST_LENGTH];
/* Hash the text. This is using pbkdf (https://en.wikipedia.org/wiki/PBKDF2). This is a common, modern way
* to hash passwords that is resistent against a number of attacks.
*/
int result = CCKeyDerivationPBKDF(kCCPBKDF2, input_data.bytes, input_data.length, salt, SALT_LENGTH, kCCPRFHmacAlgSHA512,
1000, output_data.mutableBytes, output_data.length);
if (result != 0) {
return nil;
}
NSMutableString *output = [[NSMutableString alloc] initWithString:arrayToHex(salt, SALT_LENGTH)];
[output appendString:@":"];
[output appendString:arrayToHex(output_data.bytes, output_data.length)];
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment