A Hash is a Hash algorithm that transforms an input of arbitrary length into an output of fixed length, which is the Hash value. This transformation is a compression mapping, that is, the space of hash values is usually much smaller than the space of input, and different inputs may be hashed into the same output, so it is impossible to determine a unique input value from the hash value. Simply put, it is a function that compresses a message of any length into a message digest of a fixed length

The characteristics of the Hash

  • The algorithm is public
  • The same data, you get the same answer
  • The default result of different data operations, such as MD5, is 128 bits,32 characters (hexadecimal identifier).
  • You can’t reverse this thing
  • Summary of information, information “fingerprint”, is used to do data identification.
  • Because of the length of the result it is likely that the result will be the same abbreviated hash collision

use

  • Encryption of user passwords
  • Search engine
  • copyright
  • A digital signature

Password encryption

  • The user password is encrypted using the HASH algorithm.

  • Why can’t you use RSA to encrypt the password, because if you use RSA to encrypt the plaintext will be stored on the server. This does not guarantee that the server password if written malicious theft will result in the leakage of the user password so neither the server nor the front-end can store plaintext

  • Character string ->MD5 encryption

  • (String + salt)->MD5 encryption

  • HMAC encryption salt provided by the server does not write a dead account a Key

  • (key+ string)MD5 encryption followed by timestamp and MD5 encryption

A digital signature

  • Purpose: Verify that binary data is issued by the original issuer.
  • Technology: How can I tell if the data has been modified
  • Process:
    • 1. Data may be modified during transmission
    • 2. Data + Hash value of data
    • 3. Use the same algorithm to calculate the hash value of the data to check whether the data is modified. If the hash value is different, the data is modified.
    • 4. The hash value is encrypted using RSA
    • 5. The client decrypts RSA and compares the HASH value with the HASH value of the data using the same algorithm.
    • This ensures the reliability of data transmission

Nsstrings + Hash class

// // NSString+ hash. h // EncryptDemo // // Created by h on 2018/10/16. // Copyright © 2018 Hank. All Rights Reserved. // #import <Foundation/ foundation.h > @interface NSString (Hash) #pragma mark - Hash function * @code * MD5-s "string" * @endCode * * < P > Tip: With the advent of the MD5 collision generator, the MD5 algorithm should not be used for any software integrity checking or code signing purposes. <p> * * @return MD5 hash string of 32 characters */ - (NSString *)md5String; /** * Calculate the result of SHA1 hash ** terminal test command: * @ code * echo -n "string" | openssl @ endcode sha1 * * * @ return 40 characters * / sha1 hash strings - (nsstrings *) sha1String; /** * Calculate SHA256 hash result ** terminal test command: * @ code * echo -n "string" | openssl @ endcode sha256 * * * @ return 64 sha256 hash character string * / - (nsstrings *) sha256String; /** * Calculate SHA 512 hash result ** terminal test command: * @ code * echo -n "string" | openssl @ endcode sha512 * * * @ return 128 characters of SHA hash string * 512 / - (nsstrings *) sha512String; Pragma mark HMAC hash function pragma mark HMAC hash function pragma mark HMAC hash function * @ code * echo -n "string" | openssl DGST - md5 - hmac "key" * @ endcode * * @ return 32 characters of hmac md5 hash string * / - (nsstrings *)hmacMD5StringWithKey:(NSString *)key; /** * Calculate HMAC SHA1 hash result ** Terminal test command: * @ code * echo -n "string" | openssl sha1 hmac - "key" * @ endcode * * @ return 40 characters of hmac sha1 hash string * / - (nsstrings *)hmacSHA1StringWithKey:(NSString *)key; /** * Calculate HMAC SHA256 hash result ** Terminal test command: * @ code * echo -n "string" | openssl sha256 hmac - "key" * @ endcode * * @ return 64 characters of hmac sha256 hash string * / - (nsstrings *)hmacSHA256StringWithKey:(NSString *)key; /** * Calculate HMAC SHA512 hash result ** Terminal test command: * @ code * echo -n "string" | openssl sha512 hmac - "key" * @ endcode * * @ return 128 characters of hmac sha512 hash string * / - (nsstrings *)hmacSHA512StringWithKey:(NSString *)key; Pragma mark - File hash function /** * Calculates the MD5 hash result of a file ** terminal test command: * @code * MD5 file.dat * @endCode * * @return md5 hash string of 32 characters */ - (NSString *)fileMD5Hash; /** * Calculate the SHA1 hash result of the file ** terminal test command: * @code * openssl sha1 file.dat * @endCode * * @return sha1 hash string of 40 characters */ - (NSString *)fileSHA1Hash; /** * Calculate the SHA256 hash result of the file ** terminal test command: * @code * openssl sha256 file.dat * @endCode * * @return 64 characters sha256 hash string */ - (NSString *)fileSHA256Hash; /** * Calculate the SHA512 hash result of the file ** terminal test command: * @code * openssl sha512 file.dat * @endCode * * @return 128-character SHA512 hash */ - (NSString *)fileSHA512Hash; @endCopy the code
//
//  NSString+Hash.m
//  EncryptDemo
//
//  Created by H on 2018/10/16.
//  Copyright © 2018年 hank. All rights reserved.
//

#import "NSString+Hash.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation NSString (Hash)
    
#pragma mark - 散列函数
- (NSString *)md5String {
    const char *str = self.UTF8String;
    uint8_t buffer[CC_MD5_DIGEST_LENGTH];
    
    CC_MD5(str, (CC_LONG)strlen(str), buffer);
    
    return [self stringFromBytes:buffer length:CC_MD5_DIGEST_LENGTH];
}
    
- (NSString *)sha1String {
    const char *str = self.UTF8String;
    uint8_t buffer[CC_SHA1_DIGEST_LENGTH];
    
    CC_SHA1(str, (CC_LONG)strlen(str), buffer);
    
    return [self stringFromBytes:buffer length:CC_SHA1_DIGEST_LENGTH];
}
    
- (NSString *)sha256String {
    const char *str = self.UTF8String;
    uint8_t buffer[CC_SHA256_DIGEST_LENGTH];
    
    CC_SHA256(str, (CC_LONG)strlen(str), buffer);
    
    return [self stringFromBytes:buffer length:CC_SHA256_DIGEST_LENGTH];
}
    
- (NSString *)sha512String {
    const char *str = self.UTF8String;
    uint8_t buffer[CC_SHA512_DIGEST_LENGTH];
    
    CC_SHA512(str, (CC_LONG)strlen(str), buffer);
    
    return [self stringFromBytes:buffer length:CC_SHA512_DIGEST_LENGTH];
}
    
#pragma mark - HMAC 散列函数
- (NSString *)hmacMD5StringWithKey:(NSString *)key {
    const char *keyData = key.UTF8String;
    const char *strData = self.UTF8String;
    uint8_t buffer[CC_MD5_DIGEST_LENGTH];
    
    CCHmac(kCCHmacAlgMD5, keyData, strlen(keyData), strData, strlen(strData), buffer);
    
    return [self stringFromBytes:buffer length:CC_MD5_DIGEST_LENGTH];
}
    
- (NSString *)hmacSHA1StringWithKey:(NSString *)key {
    const char *keyData = key.UTF8String;
    const char *strData = self.UTF8String;
    uint8_t buffer[CC_SHA1_DIGEST_LENGTH];
    
    CCHmac(kCCHmacAlgSHA1, keyData, strlen(keyData), strData, strlen(strData), buffer);
    
    return [self stringFromBytes:buffer length:CC_SHA1_DIGEST_LENGTH];
}
    
- (NSString *)hmacSHA256StringWithKey:(NSString *)key {
    const char *keyData = key.UTF8String;
    const char *strData = self.UTF8String;
    uint8_t buffer[CC_SHA256_DIGEST_LENGTH];
    
    CCHmac(kCCHmacAlgSHA256, keyData, strlen(keyData), strData, strlen(strData), buffer);
    
    return [self stringFromBytes:buffer length:CC_SHA256_DIGEST_LENGTH];
}
    
- (NSString *)hmacSHA512StringWithKey:(NSString *)key {
    const char *keyData = key.UTF8String;
    const char *strData = self.UTF8String;
    uint8_t buffer[CC_SHA512_DIGEST_LENGTH];
    
    CCHmac(kCCHmacAlgSHA512, keyData, strlen(keyData), strData, strlen(strData), buffer);
    
    return [self stringFromBytes:buffer length:CC_SHA512_DIGEST_LENGTH];
}
    
#pragma mark - 文件散列函数
    
#define FileHashDefaultChunkSizeForReadingData 4096
    
- (NSString *)fileMD5Hash {
    NSFileHandle *fp = [NSFileHandle fileHandleForReadingAtPath:self];
    if (fp == nil) {
        return nil;
    }
    
    CC_MD5_CTX hashCtx;
    CC_MD5_Init(&hashCtx);
    
    while (YES) {
        @autoreleasepool {
            NSData *data = [fp readDataOfLength:FileHashDefaultChunkSizeForReadingData];
            
            CC_MD5_Update(&hashCtx, data.bytes, (CC_LONG)data.length);
            
            if (data.length == 0) {
                break;
            }
        }
    }
    [fp closeFile];
    
    uint8_t buffer[CC_MD5_DIGEST_LENGTH];
    CC_MD5_Final(buffer, &hashCtx);
    
    return [self stringFromBytes:buffer length:CC_MD5_DIGEST_LENGTH];
}
    
- (NSString *)fileSHA1Hash {
    NSFileHandle *fp = [NSFileHandle fileHandleForReadingAtPath:self];
    if (fp == nil) {
        return nil;
    }
    
    CC_SHA1_CTX hashCtx;
    CC_SHA1_Init(&hashCtx);
    
    while (YES) {
        @autoreleasepool {
            NSData *data = [fp readDataOfLength:FileHashDefaultChunkSizeForReadingData];
            
            CC_SHA1_Update(&hashCtx, data.bytes, (CC_LONG)data.length);
            
            if (data.length == 0) {
                break;
            }
        }
    }
    [fp closeFile];
    
    uint8_t buffer[CC_SHA1_DIGEST_LENGTH];
    CC_SHA1_Final(buffer, &hashCtx);
    
    return [self stringFromBytes:buffer length:CC_SHA1_DIGEST_LENGTH];
}
    
- (NSString *)fileSHA256Hash {
    NSFileHandle *fp = [NSFileHandle fileHandleForReadingAtPath:self];
    if (fp == nil) {
        return nil;
    }
    
    CC_SHA256_CTX hashCtx;
    CC_SHA256_Init(&hashCtx);
    
    while (YES) {
        @autoreleasepool {
            NSData *data = [fp readDataOfLength:FileHashDefaultChunkSizeForReadingData];
            
            CC_SHA256_Update(&hashCtx, data.bytes, (CC_LONG)data.length);
            
            if (data.length == 0) {
                break;
            }
        }
    }
    [fp closeFile];
    
    uint8_t buffer[CC_SHA256_DIGEST_LENGTH];
    CC_SHA256_Final(buffer, &hashCtx);
    
    return [self stringFromBytes:buffer length:CC_SHA256_DIGEST_LENGTH];
}
    
- (NSString *)fileSHA512Hash {
    NSFileHandle *fp = [NSFileHandle fileHandleForReadingAtPath:self];
    if (fp == nil) {
        return nil;
    }
    
    CC_SHA512_CTX hashCtx;
    CC_SHA512_Init(&hashCtx);
    
    while (YES) {
        @autoreleasepool {
            NSData *data = [fp readDataOfLength:FileHashDefaultChunkSizeForReadingData];
            
            CC_SHA512_Update(&hashCtx, data.bytes, (CC_LONG)data.length);
            
            if (data.length == 0) {
                break;
            }
        }
    }
    [fp closeFile];
    
    uint8_t buffer[CC_SHA512_DIGEST_LENGTH];
    CC_SHA512_Final(buffer, &hashCtx);
    
    return [self stringFromBytes:buffer length:CC_SHA512_DIGEST_LENGTH];
}
    
#pragma mark - 助手方法
    /**
     *  返回二进制 Bytes 流的字符串表示形式
     *
     *  @param bytes  二进制 Bytes 数组
     *  @param length 数组长度
     *
     *  @return 字符串表示形式
     */
- (NSString *)stringFromBytes:(uint8_t *)bytes length:(int)length {
    NSMutableString *strM = [NSMutableString string];
    
    for (int i = 0; i < length; i++) {
        [strM appendFormat:@"%02x", bytes[i]];
    }
    
    return [strM copy];
}
    
    @end

Copy the code