Skip to content

Commit b79e9e6

Browse files
Merge f910586 into 839ed0b
2 parents 839ed0b + f910586 commit b79e9e6

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

src/file.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,10 +2069,12 @@ class File extends ServiceObject<File> {
20692069
* Example of downloading an encrypted file:
20702070
*/
20712071
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');
20762078
this.encryptionKeyHash = crypto
20772079
.createHash('sha256')
20782080
// eslint-disable-next-line @typescript-eslint/no-explicit-any

system-test/storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ describe('storage', () => {
546546
});
547547

548548
it('should set custom encryption during the upload', done => {
549-
const key = '12345678901234567890123456789012';
549+
const key = crypto.randomBytes(32);
550550
bucket.upload(
551551
FILES.big.path,
552552
{

test/file.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4175,9 +4175,8 @@ describe('File', () => {
41754175
});
41764176

41774177
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');
41814180
const KEY_HASH = crypto
41824181
.createHash('sha256')
41834182
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -4186,11 +4185,15 @@ describe('File', () => {
41864185
let _file: {};
41874186

41884187
beforeEach(() => {
4189-
_file = file.setEncryptionKey(KEY);
4188+
_file = file.setEncryptionKey(KEY_BUFFER);
41904189
});
41914190

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);
41944197
});
41954198

41964199
it('should localize the base64 key', () => {

0 commit comments

Comments
 (0)