daily pastebin goal
73%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # [javascript & java] HMAC값 생성 (sha1, base64)
  2.  
  3. ### javascript
  4. ```javascript
  5. const crypto = require('crypto');
  6.  
  7. const key = 'secret key';
  8. const data = 'data data data data data data data data data data data';
  9.  
  10. const hmac = crypto.createHmac('sha1', key);
  11. hmac.update(data);
  12. const str = hmac.digest('base64');
  13.  
  14. console.log(`result : ${str}`); // result : GWf8CRCtvePYZPxjTFqUsIoueKo=
  15. ```
  16. - node.js `crypto`모듈 사용
  17.  
  18. ### java
  19. ```java
  20. @Test
  21. public void test() throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
  22.  
  23.     String key = "secret key";
  24.     String data = "data data data data data data data data data data data";
  25.  
  26.     final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
  27.     Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
  28.     mac.init(new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM));
  29.     mac.update(data.getBytes(StandardCharsets.UTF_8.name()));
  30.     String str = Base64.encodeBase64String(mac.doFinal());
  31.  
  32.     System.out.println(String.format("result : %s", str)); // result : GWf8CRCtvePYZPxjTFqUsIoueKo=
  33.  
  34. }
  35. ```
  36. - java `javax.crypto`패키지 사용 (`Mac` Class)
RAW Paste Data
Top