cipher.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { HmacCipher } from '../src/utils/cipher/hmac'
  2. import { AESCipher } from '../src/utils/cipher/aes'
  3. const secret = {
  4. operatorSecret: 'qhtestOS73099864',
  5. dataSecret: 'qhtestDS73099864',
  6. dataSecretIV: 'qhtestIV73099864',
  7. sigSecret: 'qhtestSS73099864',
  8. }
  9. /**
  10. * Testing query_token api
  11. */
  12. import { getUTCFormatTime } from '../src/utils/time'
  13. const OperatorID = '730998640' // Requester ID
  14. const payload = {
  15. OperatorID: OperatorID,
  16. Data: undefined,
  17. TimeStamp: getUTCFormatTime(), // Need util to do this
  18. Seq: '0001',
  19. Sig: undefined,
  20. }
  21. const data = {
  22. OperatorID: OperatorID,
  23. OperatorSecret: secret.operatorSecret, // Servicer Secret
  24. }
  25. const aesCipher = new AESCipher()
  26. const hmacCipher = new HmacCipher()
  27. const encryptData = aesCipher.encrypt(
  28. JSON.stringify(data),
  29. secret.dataSecret,
  30. secret.dataSecretIV
  31. )
  32. payload.Data = encryptData
  33. const constructHashText =
  34. payload.OperatorID + payload.Data + payload.TimeStamp + payload.Seq
  35. const sign = hmacCipher.hmacMD5Sign(constructHashText, secret.sigSecret)
  36. payload.Sig = sign
  37. console.log('-'.repeat(33))
  38. console.log(JSON.stringify(payload))
  39. console.log('-'.repeat(33))
  40. const targetUrl = 'https://evtry.qihui.net/evnet/evcs/equipment/v1/query_token'
  41. fetch(targetUrl, {
  42. body: JSON.stringify(payload),
  43. headers: {
  44. 'Content-Type': 'application/json',
  45. },
  46. method: 'POST',
  47. }).then(async (resp) => {
  48. const data = await resp.json()
  49. const decryptData = aesCipher.decrypt(
  50. data.Data,
  51. secret.dataSecret,
  52. secret.dataSecretIV
  53. )
  54. console.log('data:', data)
  55. console.log('decryptData:', decryptData)
  56. // Check Sig
  57. const signature = hmacCipher.hmacMD5Sign(
  58. data.Ret + data.Msg + data.Data,
  59. secret.sigSecret
  60. )
  61. console.log('signature:', signature)
  62. console.log('Is Sig Valid:', signature === data.Sig)
  63. console.log('-'.repeat(33))
  64. })
  65. // Test crazy charge query token //
  66. // import { HmacCipher } from '../src/utils/cipher/hmac'
  67. // import { AESCipher } from '../src/utils/cipher/aes'
  68. // const secret = {
  69. // operatorSecret: 'f384603e90e46168',
  70. // dataSecret: '1ebbf89ffaaff9c6',
  71. // dataSecretIV: '8fd9154b6ef6f7c1',
  72. // sigSecret: 'a955047992ff5096',
  73. // }
  74. // /**
  75. // * Testing query_token api
  76. // */
  77. // import { getUTCFormatTime } from '../src/utils/time'
  78. // const OperatorID = '192316877' // Requester ID
  79. // const payload = {
  80. // OperatorID: OperatorID,
  81. // Data: undefined,
  82. // TimeStamp: getUTCFormatTime(), // Need util to do this
  83. // Seq: '0001',
  84. // Sig: undefined,
  85. // }
  86. // const data = {
  87. // OperatorID: OperatorID,
  88. // OperatorSecret: secret.operatorSecret, // Servicer Secret
  89. // }
  90. // const aesCipher = new AESCipher()
  91. // const hmacCipher = new HmacCipher()
  92. // const encryptData = aesCipher.encrypt(
  93. // JSON.stringify(data),
  94. // secret.dataSecret,
  95. // secret.dataSecretIV
  96. // )
  97. // payload.Data = encryptData
  98. // const constructHashText =
  99. // payload.OperatorID + payload.Data + payload.TimeStamp + payload.Seq
  100. // const sign = hmacCipher.hmacMD5Sign(constructHashText, secret.sigSecret)
  101. // payload.Sig = sign
  102. // console.log('-'.repeat(33))
  103. // console.log(JSON.stringify(payload))
  104. // console.log('-'.repeat(33))
  105. // // const targetUrl = 'http://192.168.1.33:12000/api/v1/hooks/evcs/customer/v1/query_token'
  106. // // const targetUrl =
  107. // // 'http://localhost:12000/api/v1/hooks/evcs/customer/v1/query_token'
  108. // const targetUrl =
  109. // 'https://test.crazycharge.com.hk/api/v1/hooks/evcs/customer/v1/query_token'
  110. // fetch(targetUrl, {
  111. // body: JSON.stringify(payload),
  112. // headers: {
  113. // 'Content-Type': 'application/json',
  114. // },
  115. // method: 'POST',
  116. // }).then(async (resp) => {
  117. // const data = await resp.json()
  118. // const decryptData = aesCipher.decrypt(
  119. // data.Data,
  120. // secret.dataSecret,
  121. // secret.dataSecretIV
  122. // )
  123. // console.log('data:', data)
  124. // console.log('decryptData:', decryptData)
  125. // // Check Sig
  126. // const signature = hmacCipher.hmacMD5Sign(
  127. // data.Ret + data.Msg + data.Data,
  128. // secret.sigSecret
  129. // )
  130. // console.log('signature:', signature)
  131. // console.log('Is Sig Valid:', signature === data.Sig)
  132. // console.log('-'.repeat(33))
  133. // })