You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
5.4 KiB

4 years ago
  1. # node-jwa [![Build Status](https://travis-ci.org/brianloveswords/node-jwa.svg?branch=master)](https://travis-ci.org/brianloveswords/node-jwa)
  2. A
  3. [JSON Web Algorithms](http://tools.ietf.org/id/draft-ietf-jose-json-web-algorithms-08.html)
  4. implementation focusing (exclusively, at this point) on the algorithms necessary for
  5. [JSON Web Signatures](http://self-issued.info/docs/draft-ietf-jose-json-web-signature.html).
  6. This library supports all of the required, recommended and optional cryptographic algorithms for JWS:
  7. alg Parameter Value | Digital Signature or MAC Algorithm
  8. ----------------|----------------------------
  9. HS256 | HMAC using SHA-256 hash algorithm
  10. HS384 | HMAC using SHA-384 hash algorithm
  11. HS512 | HMAC using SHA-512 hash algorithm
  12. RS256 | RSASSA using SHA-256 hash algorithm
  13. RS384 | RSASSA using SHA-384 hash algorithm
  14. RS512 | RSASSA using SHA-512 hash algorithm
  15. PS256 | RSASSA-PSS using SHA-256 hash algorithm
  16. PS384 | RSASSA-PSS using SHA-384 hash algorithm
  17. PS512 | RSASSA-PSS using SHA-512 hash algorithm
  18. ES256 | ECDSA using P-256 curve and SHA-256 hash algorithm
  19. ES384 | ECDSA using P-384 curve and SHA-384 hash algorithm
  20. ES512 | ECDSA using P-521 curve and SHA-512 hash algorithm
  21. none | No digital signature or MAC value included
  22. Please note that PS* only works on Node 6.12+ (excluding 7.x).
  23. # Requirements
  24. In order to run the tests, a recent version of OpenSSL is
  25. required. **The version that comes with OS X (OpenSSL 0.9.8r 8 Feb
  26. 2011) is not recent enough**, as it does not fully support ECDSA
  27. keys. You'll need to use a version > 1.0.0; I tested with OpenSSL 1.0.1c 10 May 2012.
  28. # Testing
  29. To run the tests, do
  30. ```bash
  31. $ npm test
  32. ```
  33. This will generate a bunch of keypairs to use in testing. If you want to
  34. generate new keypairs, do `make clean` before running `npm test` again.
  35. ## Methodology
  36. I spawn `openssl dgst -sign` to test OpenSSL sign → JS verify and
  37. `openssl dgst -verify` to test JS sign → OpenSSL verify for each of the
  38. RSA and ECDSA algorithms.
  39. # Usage
  40. ## jwa(algorithm)
  41. Creates a new `jwa` object with `sign` and `verify` methods for the
  42. algorithm. Valid values for algorithm can be found in the table above
  43. (`'HS256'`, `'HS384'`, etc) and are case-insensitive. Passing an invalid
  44. algorithm value will throw a `TypeError`.
  45. ## jwa#sign(input, secretOrPrivateKey)
  46. Sign some input with either a secret for HMAC algorithms, or a private
  47. key for RSA and ECDSA algorithms.
  48. If input is not already a string or buffer, `JSON.stringify` will be
  49. called on it to attempt to coerce it.
  50. For the HMAC algorithm, `secretOrPrivateKey` should be a string or a
  51. buffer. For ECDSA and RSA, the value should be a string representing a
  52. PEM encoded **private** key.
  53. Output [base64url](http://en.wikipedia.org/wiki/Base64#URL_applications)
  54. formatted. This is for convenience as JWS expects the signature in this
  55. format. If your application needs the output in a different format,
  56. [please open an issue](https://github.com/brianloveswords/node-jwa/issues). In
  57. the meantime, you can use
  58. [brianloveswords/base64url](https://github.com/brianloveswords/base64url)
  59. to decode the signature.
  60. As of nodejs *v0.11.8*, SPKAC support was introduce. If your nodeJs
  61. version satisfies, then you can pass an object `{ key: '..', passphrase: '...' }`
  62. ## jwa#verify(input, signature, secretOrPublicKey)
  63. Verify a signature. Returns `true` or `false`.
  64. `signature` should be a base64url encoded string.
  65. For the HMAC algorithm, `secretOrPublicKey` should be a string or a
  66. buffer. For ECDSA and RSA, the value should be a string represented a
  67. PEM encoded **public** key.
  68. # Example
  69. HMAC
  70. ```js
  71. const jwa = require('jwa');
  72. const hmac = jwa('HS256');
  73. const input = 'super important stuff';
  74. const secret = 'shhhhhh';
  75. const signature = hmac.sign(input, secret);
  76. hmac.verify(input, signature, secret) // === true
  77. hmac.verify(input, signature, 'trickery!') // === false
  78. ```
  79. With keys
  80. ```js
  81. const fs = require('fs');
  82. const jwa = require('jwa');
  83. const privateKey = fs.readFileSync(__dirname + '/ecdsa-p521-private.pem');
  84. const publicKey = fs.readFileSync(__dirname + '/ecdsa-p521-public.pem');
  85. const ecdsa = jwa('ES512');
  86. const input = 'very important stuff';
  87. const signature = ecdsa.sign(input, privateKey);
  88. ecdsa.verify(input, signature, publicKey) // === true
  89. ```
  90. ## License
  91. MIT
  92. ```
  93. Copyright (c) 2013 Brian J. Brennan
  94. Permission is hereby granted, free of charge, to any person obtaining a
  95. copy of this software and associated documentation files (the
  96. "Software"), to deal in the Software without restriction, including
  97. without limitation the rights to use, copy, modify, merge, publish,
  98. distribute, sublicense, and/or sell copies of the Software, and to
  99. permit persons to whom the Software is furnished to do so, subject to
  100. the following conditions:
  101. The above copyright notice and this permission notice shall be included
  102. in all copies or substantial portions of the Software.
  103. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  104. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  105. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  106. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  107. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  108. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  109. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  110. ```