HASH
To transform an arbitrary length of input into a fixed length of output using a HASH algorithm. The output is called the HASH value. This transformation is a compression mapping, that is, the space of the hash values is usually much smaller than the space of the input values, and different inputs may hash into the same output, so it is not possible to determine the unique input values from the hash values. In simple terms, a message of any length is compressed into a message digest of a fixed length
-
Hashes have the following characteristics:
- Algorithm in public
- You run the same data, you get the same result
- To operate on different data such as
MD5
, the default result is128
position32
A character (16
Into the system) - You can’t reverse it
- Summary of information for data identification
-
Examples of HASH Algorithms
MD5
128 bits, 32 hexadecimal charactersSHA1
160 bits, 40 hexadecimal charactersSHA256
256 characters, 64 hexadecimal characters
-
Starting with iOS 13.0, Apple recommends scrapping MD5 in favor of SHA256 or more secure encryption.
-
Examples of symmetric encryption algorithms
DES
Use less, low strength.3DES
If three secret keys are used to encrypt the original data for three times, it is meaninglessAES
Advanced encryption standard, widely used. Apple keychain access, NSA, etc
-
HASH Algorithm Usage
- Encryption of the user password
- Search engine
- copyright
- A digital signature
-
Encryption of the user password
- When a user registers, the server stores the password directly
HASH
value - When a user logs in, set the password to
HASH
Form is passed to the server - Due to the same object
HASH
The values are always the same, and now there are trillions of petabytes of themHASH
Large database, can passHASH
Query the original value. - Multiple nested
HASH
Or add salt processing, also will be large database collision query answer (nested can recursive collision, salt will leak)
- When a user registers, the server stores the password directly
-
A more secure
HMAC
Encryption scheme- The encryption is done with a secret key and the hash is done twice. In the actual development, the secret key comes from the server.
- The key is generated by the server account during registration and sent to the client.
- The key can be used to enable the device lock. If the local device does not have the key, ask the server for the key and the server can determine whether to deliver it. If the device lock is enabled, perform operations on the original device.
-
Pay attention to,
HAMC
The value processed by the encryption scheme can still be intercepted in network transmission and is still insecure. -
Safer options:
- When a user logs in, the
HAMC
After the encrypted password is spliced, the time stamp (up to the minute bit) is repeatedMD5
Encryption. When the server receives the request, it removes the user’s from the libraryHASH
Password, splicing the server timestamp onceMD5
Encryption, if the server results do not match the client, then the server forwards the timestamp one minute, matches again, and allows login if successful. - In this case, the user’s
HASH
The maximum validity period of a password is1 minutes and 59 seconds
, which can prevent hidden dangers caused by network interception.
- When a user logs in, the
-
Search engine
- Search engine split search, every word of
HASH
You add them, you get the same result in any order.
- Search engine split search, every word of
-
copyright
- For cloud disk applications, data can be uploaded with cloud resources
HASH
Value, and then a series of algorithms (because the cloud resources are huge, andHASH
Values can collide, that is, the data is different,HASH
Value is the same), such as the file binary front, middle and backN
If the data matches successfully, the data can be identified as the same data, and the data is directly transmitted in seconds (reference unified resources). - When the user uploads resources, the original file
HASH
Value that can be used to verify ownership. HASH
The value is used to calculate binary data of the file, and the file name will not be changedHASH
Value has any effect.- Compressed file
HASH
The value will change because the binary of the file has changed to copy the fileHASH
Will not change.
- For cloud disk applications, data can be uploaded with cloud resources
-
A digital signature
- Will the original data
HASH
The valueRSA
Encryption, will be encrypted afterHASH
The value is passed to the server along with the original data. - The server gets the data after the original
HASH
The valueRSA
Decrypt, if the decryptedHASH
Value and the data passed by the clientHASH
Value match, then the original data is untampered, otherwise the original data is tampered, the server needs to reject the response. - This kind of use
RSA
The encryptedHASH
Value is the digital signature.
- Will the original data
-
Symmetric Encryption (traditional Encryption)
-
Application mode
ECB
(Electronic Code Book
): Electronic password book mode, each piece of data, independent encryption.- The most basic encryption mode, the same plaintext will always be encrypted into the same ciphertext, no initial vector, vulnerable to cipher book replay attacks, generally rarely used.
CBC
(Cipher Block Chaining
): Cryptographic block link mode, using a secret key and an initialization vector to encrypt data.- Before encrypting, the plaintext must perform xOR operation with the preceding ciphertext. Therefore, as long as different initial vectors are selected, the same plaintext will form different ciphertext after encryption. This is the most extensive encryption mode at present.
CBC
Encrypted ciphertext is context-dependent. Plaintext errors are not transmitted to subsequent packets, but if one packet is lost, all subsequent packets are invalidated (synchronization errors). CBC
This ensures the integrity of ciphertext. If data is lost or tampered during transmission, subsequent data cannot be decrypted.
- Before encrypting, the plaintext must perform xOR operation with the preceding ciphertext. Therefore, as long as different initial vectors are selected, the same plaintext will form different ciphertext after encryption. This is the most extensive encryption mode at present.
-
Terminal Experience encryption
openssl enc -des-ecb -K 123 -nosalt -in before.txt -out after.bin
DES encryption, ECB mode, key 123, no saltopenssl enc -des-cbc -iv 0102030405060708 -K 123 -nosalt -in before.txt -out after.bin
DES encryption, CBC mode, key is 123, the initial vector is 0102030405060708, no salt- Parameters to increase
-d
To decrypt
-
Symmetric encryption in iOS
#import <CommonCrypto/CommonCrypto.h>
A library that handles encryption and decryption in iOSCCCrypt
: Functions used for encryption and decryption in iOS, as follows:
CCCryptorStatus CCCrypt(
CCOperation op,
CCAlgorithm alg,
CCOptions options,
const void *key,
size_t keyLength,
const void *iv,
const void *dataIn,
size_t dataInLength,
void *dataOut,
size_t dataOutAvailable,
size_t *dataOutMoved)API_AVAILABLE(macos(10.4), ios(2.0));
Copy the code