crypto-js#HmacSHA1 TypeScript Examples

The following examples show how to use crypto-js#HmacSHA1. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: signature.ts    From reactjs-social-login with MIT License 6 votes vote down vote up
makeSignature = (
  params: any,
  method: string,
  apiUrl: string,
  consumerSecret: string,
  oauthSecret = ''
) => {
  const paramsBaseString = Object.keys(params)
    .sort()
    .reduce((prev: string, el: any) => {
      return (prev += `&${el}=${params[el]}`)
    }, '')
    .substr(1)

  const signatureBaseString = `${method.toUpperCase()}&${encodeURIComponent(
    apiUrl
  )}&${encodeURIComponent(paramsBaseString)}`

  const signingKey = `${encodeURIComponent(
    consumerSecret
  )}&${encodeURIComponent(oauthSecret)}`

  const oauthSignature = enc.Base64.stringify(
    HmacSHA1(signatureBaseString, signingKey)
  )

  const paramsWithSignature = {
    ...params,
    oauth_signature: encodeURIComponent(oauthSignature)
  }

  return Object.keys(paramsWithSignature)
    .sort()
    .reduce((prev: string, el: any) => {
      return (prev += `,${el}="${paramsWithSignature[el]}"`)
    }, '')
    .substr(1)
}