Implementing HMAC encryption algorithm in iPhone application

前端 未结 2 793
醉话见心
醉话见心 2021-01-31 06:23

I want to implement HMAC encryption algorithm for my iPhone application. Any sample code will really help. Also, please guide me with brief implementation of the same.

2条回答
  •  余生分开走
    2021-01-31 06:55

    HMAC is not an encryption mechanism, but an authentication digest. It uses an underlying message digest function such as SHA-1, SHA-256, MD5 etc, with a secret key to generate a code that can be used to authenticate data.

    Generating an HMAC digest is extremely simple. Here is the description from RFC2104 (via Wikipedia)

    Let:

    • H(·) be a cryptographic hash function (ie. SHA-1, SHA-256, MD5 etc)
    • K be a secret key padded to the right with extra zeros to the input block size of the hash function, or the hash of the original key if it's longer than that block size
    • m be the message to be authenticated
    • | denote concatenation
    • ⊕ denote exclusive or (XOR)
    • opad be the outer padding (0x5c5c5c…5c5c, one-block-long hexadecimal constant)
    • ipad be the inner padding (0x363636…3636, one-block-long hexadecimal constant)

    Then HMAC(K,m) is mathematically defined by:

    HMAC(K,m) = H((K ⊕ opad) | H((K ⊕ ipad) | m)).

    For the underlying digest function you can help yourself to one of the C implementations from OpenSSL. In fact it also has a C implementation of HMAC that you can probably just use as is.

提交回复
热议问题