crypto: add crypto.sign() and crypto.verify() · nodejs/node@7d0e50d (original) (raw)

`@@ -2,10 +2,16 @@

`

2

2

``

3

3

`const {

`

4

4

`ERR_CRYPTO_SIGN_KEY_REQUIRED,

`

``

5

`+

ERR_INVALID_ARG_TYPE,

`

5

6

`ERR_INVALID_OPT_VALUE

`

6

7

`} = require('internal/errors').codes;

`

7

8

`const { validateString } = require('internal/validators');

`

8

``

`-

const { Sign: _Sign, Verify: _Verify } = internalBinding('crypto');

`

``

9

`+

const {

`

``

10

`+

Sign: _Sign,

`

``

11

`+

Verify: _Verify,

`

``

12

`+

signOneShot: _signOneShot,

`

``

13

`+

verifyOneShot: _verifyOneShot

`

``

14

`+

} = internalBinding('crypto');

`

9

15

`const {

`

10

16

`RSA_PSS_SALTLEN_AUTO,

`

11

17

`RSA_PKCS1_PADDING

`

`@@ -22,6 +28,7 @@ const {

`

22

28

` preparePublicOrPrivateKey

`

23

29

`} = require('internal/crypto/keys');

`

24

30

`const { Writable } = require('stream');

`

``

31

`+

const { isArrayBufferView } = require('internal/util/types');

`

25

32

``

26

33

`function Sign(algorithm, options) {

`

27

34

`if (!(this instanceof Sign))

`

`@@ -91,6 +98,35 @@ Sign.prototype.sign = function sign(options, encoding) {

`

91

98

`return ret;

`

92

99

`};

`

93

100

``

``

101

`+

function signOneShot(algorithm, data, key) {

`

``

102

`+

if (algorithm != null)

`

``

103

`+

validateString(algorithm, 'algorithm');

`

``

104

+

``

105

`+

if (!isArrayBufferView(data)) {

`

``

106

`+

throw new ERR_INVALID_ARG_TYPE(

`

``

107

`+

'data',

`

``

108

`+

['Buffer', 'TypedArray', 'DataView'],

`

``

109

`+

data

`

``

110

`+

);

`

``

111

`+

}

`

``

112

+

``

113

`+

if (!key)

`

``

114

`+

throw new ERR_CRYPTO_SIGN_KEY_REQUIRED();

`

``

115

+

``

116

`+

const {

`

``

117

`+

data: keyData,

`

``

118

`+

format: keyFormat,

`

``

119

`+

type: keyType,

`

``

120

`+

passphrase: keyPassphrase

`

``

121

`+

} = preparePrivateKey(key);

`

``

122

+

``

123

`+

// Options specific to RSA

`

``

124

`+

const rsaPadding = getPadding(key);

`

``

125

`+

const pssSaltLength = getSaltLength(key);

`

``

126

+

``

127

`+

return _signOneShot(keyData, keyFormat, keyType, keyPassphrase, data,

`

``

128

`+

algorithm, rsaPadding, pssSaltLength);

`

``

129

`+

}

`

94

130

``

95

131

`function Verify(algorithm, options) {

`

96

132

`if (!(this instanceof Verify))

`

`@@ -132,7 +168,44 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) {

`

132

168

``

133

169

`legacyNativeHandle(Verify);

`

134

170

``

``

171

`+

function verifyOneShot(algorithm, data, key, signature) {

`

``

172

`+

if (algorithm != null)

`

``

173

`+

validateString(algorithm, 'algorithm');

`

``

174

+

``

175

`+

if (!isArrayBufferView(data)) {

`

``

176

`+

throw new ERR_INVALID_ARG_TYPE(

`

``

177

`+

'data',

`

``

178

`+

['Buffer', 'TypedArray', 'DataView'],

`

``

179

`+

data

`

``

180

`+

);

`

``

181

`+

}

`

``

182

+

``

183

`+

const {

`

``

184

`+

data: keyData,

`

``

185

`+

format: keyFormat,

`

``

186

`+

type: keyType,

`

``

187

`+

passphrase: keyPassphrase

`

``

188

`+

} = preparePublicOrPrivateKey(key);

`

``

189

+

``

190

`+

// Options specific to RSA

`

``

191

`+

const rsaPadding = getPadding(key);

`

``

192

`+

const pssSaltLength = getSaltLength(key);

`

``

193

+

``

194

`+

if (!isArrayBufferView(signature)) {

`

``

195

`+

throw new ERR_INVALID_ARG_TYPE(

`

``

196

`+

'signature',

`

``

197

`+

['Buffer', 'TypedArray', 'DataView'],

`

``

198

`+

signature

`

``

199

`+

);

`

``

200

`+

}

`

``

201

+

``

202

`+

return _verifyOneShot(keyData, keyFormat, keyType, keyPassphrase, signature,

`

``

203

`+

data, algorithm, rsaPadding, pssSaltLength);

`

``

204

`+

}

`

``

205

+

135

206

`module.exports = {

`

136

207

` Sign,

`

137

``

`-

Verify

`

``

208

`+

signOneShot,

`

``

209

`+

Verify,

`

``

210

`+

verifyOneShot

`

138

211

`};

`