改进,使得传入参数 key 可以 undefined
This commit is contained in:
		
							parent
							
								
									3f63179414
								
							
						
					
					
						commit
						17bbd40ee1
					
				
							
								
								
									
										17
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								index.js
									
									
									
									
									
								
							| @ -5,15 +5,28 @@ const crypto = require('crypto') | |||||||
| module.exports = { | module.exports = { | ||||||
|   createToken: function (content, key) { |   createToken: function (content, key) { | ||||||
|     // content 可以是数字,非空字符串或非空对象,不可以是数组。
 |     // content 可以是数字,非空字符串或非空对象,不可以是数组。
 | ||||||
|  |     // key 可以未定义,则默认设为空字符串,再转化为哈希。(jsonwebtoken 要求 key 必须有值)
 | ||||||
|     try { |     try { | ||||||
|       return JsonWebToken.sign(content, crypto.createHash('sha256').update(key, 'utf8').digest('hex')) |       return JsonWebToken.sign( | ||||||
|  |         content, | ||||||
|  |         crypto | ||||||
|  |           .createHash('sha256') | ||||||
|  |           .update(key || '', 'utf8') | ||||||
|  |           .digest('hex') | ||||||
|  |       ) | ||||||
|     } catch (exp) { |     } catch (exp) { | ||||||
|       return null |       return null | ||||||
|     } |     } | ||||||
|   }, |   }, | ||||||
|   verifyToken: function (token, key) { |   verifyToken: function (token, key) { | ||||||
|     try { |     try { | ||||||
|       return JsonWebToken.verify(token, crypto.createHash('sha256').update(key, 'utf8').digest('hex')) |       return JsonWebToken.verify( | ||||||
|  |         token, | ||||||
|  |         crypto | ||||||
|  |           .createHash('sha256') | ||||||
|  |           .update(key || '', 'utf8') | ||||||
|  |           .digest('hex') | ||||||
|  |       ) | ||||||
|     } catch (exp) { |     } catch (exp) { | ||||||
|       return null |       return null | ||||||
|     } |     } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user