Skip to content

fix: allow string encryption keys #1418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2069,10 +2069,12 @@ class File extends ServiceObject<File> {
* Example of downloading an encrypted file:
*/
setEncryptionKey(encryptionKey: string | Buffer) {
this.encryptionKey = encryptionKey;
this.encryptionKeyBase64 = Buffer.from(encryptionKey as string).toString(
'base64'
);
if (Buffer.isBuffer(encryptionKey)) {
this.encryptionKey = encryptionKey;
} else {
this.encryptionKey = Buffer.from(encryptionKey, 'base64');
}
this.encryptionKeyBase64 = this.encryptionKey.toString('base64');
this.encryptionKeyHash = crypto
.createHash('sha256')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
2 changes: 1 addition & 1 deletion system-test/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ describe('storage', () => {
});

it('should set custom encryption during the upload', done => {
const key = '12345678901234567890123456789012';
const key = crypto.randomBytes(32);
bucket.upload(
FILES.big.path,
{
Expand Down
15 changes: 9 additions & 6 deletions test/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4175,9 +4175,8 @@ describe('File', () => {
});

describe('setEncryptionKey', () => {
const KEY = crypto.randomBytes(32);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const KEY_BASE64 = Buffer.from(KEY as any).toString('base64');
const KEY_BUFFER = crypto.randomBytes(32);
const KEY_BASE64 = KEY_BUFFER.toString('base64');
const KEY_HASH = crypto
.createHash('sha256')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -4186,11 +4185,15 @@ describe('File', () => {
let _file: {};

beforeEach(() => {
_file = file.setEncryptionKey(KEY);
_file = file.setEncryptionKey(KEY_BUFFER);
});

it('should localize the key', () => {
assert.strictEqual(file.encryptionKey, KEY);
it('should localize the key as a Buffer', () => {
file.setEncryptionKey(KEY_BUFFER);
assert.deepStrictEqual(file.encryptionKey, KEY_BUFFER);

file.setEncryptionKey(KEY_BASE64);
assert.deepStrictEqual(file.encryptionKey, KEY_BUFFER);
});

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