File tree Expand file tree Collapse file tree 3 files changed +16
-11
lines changed Expand file tree Collapse file tree 3 files changed +16
-11
lines changed Original file line number Diff line number Diff line change @@ -2069,10 +2069,12 @@ class File extends ServiceObject<File> {
2069
2069
* Example of downloading an encrypted file:
2070
2070
*/
2071
2071
setEncryptionKey ( encryptionKey : string | Buffer ) {
2072
- this . encryptionKey = encryptionKey ;
2073
- this . encryptionKeyBase64 = Buffer . from ( encryptionKey as string ) . toString (
2074
- 'base64'
2075
- ) ;
2072
+ if ( Buffer . isBuffer ( encryptionKey ) ) {
2073
+ this . encryptionKey = encryptionKey ;
2074
+ } else {
2075
+ this . encryptionKey = Buffer . from ( encryptionKey , 'base64' ) ;
2076
+ }
2077
+ this . encryptionKeyBase64 = this . encryptionKey . toString ( 'base64' ) ;
2076
2078
this . encryptionKeyHash = crypto
2077
2079
. createHash ( 'sha256' )
2078
2080
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Original file line number Diff line number Diff line change @@ -546,7 +546,7 @@ describe('storage', () => {
546
546
} ) ;
547
547
548
548
it ( 'should set custom encryption during the upload' , done => {
549
- const key = '12345678901234567890123456789012' ;
549
+ const key = crypto . randomBytes ( 32 ) ;
550
550
bucket . upload (
551
551
FILES . big . path ,
552
552
{
Original file line number Diff line number Diff line change @@ -4175,9 +4175,8 @@ describe('File', () => {
4175
4175
} ) ;
4176
4176
4177
4177
describe ( 'setEncryptionKey' , ( ) => {
4178
- const KEY = crypto . randomBytes ( 32 ) ;
4179
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
4180
- const KEY_BASE64 = Buffer . from ( KEY as any ) . toString ( 'base64' ) ;
4178
+ const KEY_BUFFER = crypto . randomBytes ( 32 ) ;
4179
+ const KEY_BASE64 = KEY_BUFFER . toString ( 'base64' ) ;
4181
4180
const KEY_HASH = crypto
4182
4181
. createHash ( 'sha256' )
4183
4182
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -4186,11 +4185,15 @@ describe('File', () => {
4186
4185
let _file : { } ;
4187
4186
4188
4187
beforeEach ( ( ) => {
4189
- _file = file . setEncryptionKey ( KEY ) ;
4188
+ _file = file . setEncryptionKey ( KEY_BUFFER ) ;
4190
4189
} ) ;
4191
4190
4192
- it ( 'should localize the key' , ( ) => {
4193
- assert . strictEqual ( file . encryptionKey , KEY ) ;
4191
+ it ( 'should localize the key as a Buffer' , ( ) => {
4192
+ file . setEncryptionKey ( KEY_BUFFER ) ;
4193
+ assert . deepStrictEqual ( file . encryptionKey , KEY_BUFFER ) ;
4194
+
4195
+ file . setEncryptionKey ( KEY_BASE64 ) ;
4196
+ assert . deepStrictEqual ( file . encryptionKey , KEY_BUFFER ) ;
4194
4197
} ) ;
4195
4198
4196
4199
it ( 'should localize the base64 key' , ( ) => {
You can’t perform that action at this time.
0 commit comments