@@ -4,6 +4,7 @@ var Buffer = require('safe-buffer').Buffer;
44var Transform = require ( 'stream' ) . Transform ;
55var StringDecoder = require ( 'string_decoder' ) . StringDecoder ;
66var inherits = require ( 'inherits' ) ;
7+ var toBuffer = require ( 'to-buffer' ) ;
78
89function CipherBase ( hashMode ) {
910 Transform . call ( this ) ;
@@ -22,71 +23,6 @@ function CipherBase(hashMode) {
2223}
2324inherits ( CipherBase , Transform ) ;
2425
25- var useUint8Array = typeof Uint8Array !== 'undefined' ;
26- var useArrayBuffer = typeof ArrayBuffer !== 'undefined'
27- && typeof Uint8Array !== 'undefined'
28- && ArrayBuffer . isView
29- && ( Buffer . prototype instanceof Uint8Array || Buffer . TYPED_ARRAY_SUPPORT ) ;
30-
31- function toBuffer ( data , encoding ) {
32- /*
33- * No need to do anything for exact instance
34- * This is only valid when safe-buffer.Buffer === buffer.Buffer, i.e. when Buffer.from/Buffer.alloc existed
35- */
36- if ( data instanceof Buffer ) {
37- return data ;
38- }
39-
40- // Convert strings to Buffer
41- if ( typeof data === 'string' ) {
42- return Buffer . from ( data , encoding ) ;
43- }
44-
45- /*
46- * Wrap any TypedArray instances and DataViews
47- * Makes sense only on engines with full TypedArray support -- let Buffer detect that
48- */
49- if ( useArrayBuffer && ArrayBuffer . isView ( data ) ) {
50- // Bug in Node.js <6.3.1, which treats this as out-of-bounds
51- if ( data . byteLength === 0 ) {
52- return Buffer . alloc ( 0 ) ;
53- }
54-
55- var res = Buffer . from ( data . buffer , data . byteOffset , data . byteLength ) ;
56- /*
57- * Recheck result size, as offset/length doesn't work on Node.js <5.10
58- * We just go to Uint8Array case if this fails
59- */
60- if ( res . byteLength === data . byteLength ) {
61- return res ;
62- }
63- }
64-
65- /*
66- * Uint8Array in engines where Buffer.from might not work with ArrayBuffer, just copy over
67- * Doesn't make sense with other TypedArray instances
68- */
69- if ( useUint8Array && data instanceof Uint8Array ) {
70- return Buffer . from ( data ) ;
71- }
72-
73- /*
74- * Old Buffer polyfill on an engine that doesn't have TypedArray support
75- * Also, this is from a different Buffer polyfill implementation then we have, as instanceof check failed
76- * Convert to our current Buffer implementation
77- */
78- if (
79- Buffer . isBuffer ( data )
80- && data . constructor
81- && typeof data . constructor . isBuffer === 'function'
82- && data . constructor . isBuffer ( data )
83- ) {
84- return Buffer . from ( data ) ;
85- }
86-
87- throw new TypeError ( 'The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView.' ) ;
88- }
89-
9026CipherBase . prototype . update = function ( data , inputEnc , outputEnc ) {
9127 var bufferData = toBuffer ( data , inputEnc ) ; // asserts correct input type
9228 var outData = this . _update ( bufferData ) ;
0 commit comments