Network security--base64 Encoding, MD5, sha1-sha512, HMAC (sha1-sha512) hash

Source: Internet
Author: User
Tags base64 base64 encode hmac md5 openssl readable sha1

Read Catalogue

    • First, Base64 code
    • Second, MD5, SHA1, SHA256, SHA512, HMAC implementation
    • GitHub Code

It is said that today 520 is a good day, why I think of 502, 500, 404 these? Fortunately the server is OK!

First, Base64 code

The BASE64 encoding requires the conversion of 3 8-bit bytes (3*8=24) to 4 6-bit bytes (4*6=24) , followed by 6 two in front of 0 bits, forming a 8-bit byte form, so that the valid bit for each byte is 6 bits, then the value range is 0~63 0 ~ (2^6 - 1) . If the last remaining character is less than 3 bytes, it is filled with 0 and the output character uses ' = ', so we see that there are 1 to 2 ' = ' At the end of the Base64. In addition, the standard requires every 76 characters to insert a newline (However, this depends on the case).

IOS7 after Apple has its own Base64 codec Api,nsdata extension:NSData (NSDataBase64Encoding)

Two kinds of storage methods
    • Visible string Form

In order to ensure that each encoded byte of the output is a readable character, rather than 0~63 these numbers, Base64 makes a code table that, like the ASCII table, has corresponding characters for each Base64 code value. 64 readable characters from 0 to 63 No, this is the A-Z、a-z、0-9、+、/ origin of the Base64 name.

    • In 16 binary form

That is, the NSData form is saved, the BASE64 encoding result is a character, and these characters correspond to the code value of the ASCII code table, NSData is the code value that stores the ASCII code table.

Here is an example of how the BASE64 encoding and decoding process is described in detail in the API provided by Apple:

Suppose we Base64 encode the string "123", the corresponding 16 binary of "123" is 313233, the binary is 00110001、00110010、00110011 , it becomes the 4*6 result is the first row in the following table. Then according to the Base64 's code table, they correspond to the second row in the table, respectively. Then the final result of the "123" encoding is Mtiz, which is saved as a string. The ASCII code value is then stored in nsdata form according to the Mtiz, such as the third row in the table.

Convert to 4*6 results 00001100 00010011 00001000 00110011
Base64 corresponding characters M T I Z
ASCII code value (16 binary) 4d 54 49 7a

The above procedure is implemented by code as follows:

// 1 待编码的原始字符串NSString *plainStr = @"123";// 2 将其转换成NSData保存,那么"123"对应的ASCII码表码值是31、32、33(16进制)NSData *plainData = [plainStr dataUsingEncoding:NSUTF8StringEncoding];// 3.1 将其进行Base64编码,且结果以字符串形式保存,对应表中的第二行NSString *baseStr = [plainData base64EncodedStringWithOptions:0];// 3.2 将其进行Base64编码,且结果以NSData形式保存NSData *base64Data = [plainData base64EncodedDataWithOptions:0];

In addition, for the parameter nsdatabase64encodingoptions option, there are several values

    • Nsdatabase64encoding64characterlinelength: Insert \ r or \ n per 64 characters
    • Nsdatabase64encoding76characterlinelength: Every 76 characters are inserted \ R or \ n, the standard is required to be 76 characters to wrap, but the specific or self-determined
    • Nsdatabase64encodingendlinewithcarriagereturn: the insertion character is \ r
    • Nsdatabase64encodingendlinewithlinefeed: Insert character is \ n

The first two options are whether to allow the insertion of characters, and how many characters are inserted, either one or both. The latter two options represent the specific characters to be inserted. For example, we want to 76 words specifier insert a \ r can be NSDataBase64Encoding76CharacterLineLength | NSDataBase64EncodingEndLineWithCarriageReturn . In the example above, the selected item is 0, which means the character is not inserted.

Third-party frameworks

Before iOS7 we used to be a third-party framework, such as Nicklockwood wrote Https://github.com/nicklockwood/Base64 and Google's GTMBase64, although Apple has its own implementation, But many other cryptographic frameworks use it, so it's still a bit of an idea, and it also provides arbitrary-length character insertions \r\n , and Apple can only be 64 or 76 lengths.

Second, MD5, SHA1, SHA256, SHA512, HMAC implementation

Used primarily for validation to prevent the information from being modified. Please refer to Http://www.jianshu.com/p/003b85fd3e36 for details.

Specific implementations refer to third-party frameworks: Https://github.com/kelp404/CocoaSecurity. Very comprehensive, but not too convenient, such as to get MD5 results

NSString *plainStr = @"123";CocoaSecurityResult *md5 = [CocoaSecurity md5:plainStr];// 获取md5结果NSString *md5Str = md5.hexLower;

Can not directly plainstr.md5hash to get the result of the string form, here I encapsulated a, see the project Nsstring+hash class Https://github.com/mddios/EncryptionTools, The string can be manipulated directly, similar plainStr.MD5Hash、plainStr.sha1Hash···plainStr.sha256Hash··· , very convenient.

For example, for the @ "123" hash, here are examples of the two methods mentioned above:

- (void) Hashtest {nsstring *plainstr = @"123";MD5 Cocoasecurityresult *MD5 = [cocoasecurityMD5:PLAINSTR]; NSLog (@"Md5:%lu---%@---%@", plainStr.md5Hash.length, Plainstr.md5hash,md5.hex);Cocoasecurityresult *SHA1 = [cocoasecuritySHA1:PLAINSTR]; NSLog (@"Sha1:%lu---%@---%@", plainStr.sha1Hash.length, Plainstr.sha1hash,sha1.hex);Cocoasecurityresult *sha224 = [cocoasecuritySHA224:PLAINSTR]; NSLog (@"Sha224:%lu---%@---%@", plainstr.sha224hash.length,plainstr.sha224hash,sha224.hex);Cocoasecurityresult *sha256 = [cocoasecuritySHA256:PLAINSTR]; NSLog (@"Sha256:%lu---%@---%@", plainstr.sha256hash.length,plainstr.sha256hash,sha256.hex);//Cocoasecurityresult *sha384 = [cocoasecurity sha384:plainstr]; NSLog (@"Sha384:%lu---%@---%@", plainstr.sha384hash.length,plainstr.sha384hash,sha384.hex); //Cocoasecurityresult *sha512 = [cocoasecurity sha512:plainstr]; NSLog (@"Sha512:%lu---%@---%@", plainstr.sha512hash.length,plainstr.sha512hash,sha512.hex); //HMAC Cocoasecurityresult *hmacmd5 = [cocoasecurity hmacmd5:plainstr hmackey:plainstr]; NSLog (@"Hmacmd5:%lu---%@---%@", [plainstr hmacmd5withkey:plainstr].length,[plainstr hmacmd5withkey: Plainstr],hmacmd5.hex);}           
    • Get results from a computer terminal

Encapsulated code in the NSString+Hash.h header file, there is a specific list of terminal command methods, as follows:

Return Result: 32 length terminal command: Md5-s "123"-(NSString *) Md5hash;Results returned: 40 length terminal command: Echo-n "123" | OpenSSL sha-sha1-(NSString *) Sha1hash;Results returned: 56 length terminal command: Echo-n "123" | OpenSSL sha-sha224-(NSString *) Sha224hash;Results returned: 64 length terminal command: Echo-n "123" | OpenSSL sha-sha256-(NSString *) Sha256hash;Results returned: 96 length terminal command: Echo-n "123" | OpenSSL sha-sha384-(NSString *) Sha384hash;Results returned: 128 length terminal command: Echo-n "123" | OpenSSL sha-sha512-(NSString *) Sha512hash;#pragma mark-hmac///Return Result: 32 length terminal command: Echo-n "123" | OpenSSL Dgst-md5-hmac "123"-(NSString *) Hmacmd5withke Y: (NSString *) key; / //Return Result: 40 length Echo-n "123" | OpenSSL Sha-sha1-hmac "123"-(NSString *) Hmacsha1withkey: (NSString *) key;-(
                       
                        nsstring *) Hmacsha224withkey: (
                        NSString *) key;-(NSString *) Hmacsha256withkey: (NSString *) key;-(  NSString *) Hmacsha384withkey: (NSString *) key;-(NSString *) Hmacsha512withkey: (NSString *) key;  
                       
    • About MD5 add salt, just a few more of the following first line
plainStr = [plainStr stringByAppendingString:salt];NSString *md5Str = plainStr.md5Hash;
GitHub Code

Https://github.com/mddios/EncryptionTools

Network security--base64 Encoding, MD5, sha1-sha512, HMAC (sha1-sha512) hash

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.