@@ -3,10 +3,6 @@ import { fromString } from 'uint8arrays/from-string'
3
3
import webcrypto from '../webcrypto.js'
4
4
import type { CreateOptions , AESCipher } from './interface.js'
5
5
6
- export function isWebkitLinux ( ) : boolean {
7
- return typeof navigator !== 'undefined' && navigator . userAgent . includes ( 'Safari' ) && navigator . userAgent . includes ( 'Linux' ) && ! navigator . userAgent . includes ( 'Chrome' )
8
- }
9
-
10
6
// WebKit on Linux does not support deriving a key from an empty PBKDF2 key.
11
7
// So, as a workaround, we provide the generated key as a constant. We test that
12
8
// this generated key is accurate in test/workaround.spec.ts
@@ -46,8 +42,15 @@ export function create (opts?: CreateOptions): AESCipher {
46
42
}
47
43
48
44
let cryptoKey : CryptoKey
49
- if ( password . length === 0 && isWebkitLinux ( ) ) {
45
+ if ( password . length === 0 ) {
50
46
cryptoKey = await crypto . subtle . importKey ( 'jwk' , derivedEmptyPasswordKey , { name : 'AES-GCM' } , true , [ 'encrypt' ] )
47
+ try {
48
+ const deriveParams = { name : 'PBKDF2' , salt, iterations, hash : { name : digest } }
49
+ const runtimeDerivedEmptyPassword = await crypto . subtle . importKey ( 'raw' , password , { name : 'PBKDF2' } , false , [ 'deriveKey' ] )
50
+ cryptoKey = await crypto . subtle . deriveKey ( deriveParams , runtimeDerivedEmptyPassword , { name : algorithm , length : keyLength } , true , [ 'encrypt' ] )
51
+ } catch {
52
+ cryptoKey = await crypto . subtle . importKey ( 'jwk' , derivedEmptyPasswordKey , { name : 'AES-GCM' } , true , [ 'encrypt' ] )
53
+ }
51
54
} else {
52
55
// Derive a key using PBKDF2.
53
56
const deriveParams = { name : 'PBKDF2' , salt, iterations, hash : { name : digest } }
@@ -77,8 +80,14 @@ export function create (opts?: CreateOptions): AESCipher {
77
80
}
78
81
79
82
let cryptoKey : CryptoKey
80
- if ( password . length === 0 && isWebkitLinux ( ) ) {
81
- cryptoKey = await crypto . subtle . importKey ( 'jwk' , derivedEmptyPasswordKey , { name : 'AES-GCM' } , true , [ 'decrypt' ] )
83
+ if ( password . length === 0 ) {
84
+ try {
85
+ const deriveParams = { name : 'PBKDF2' , salt, iterations, hash : { name : digest } }
86
+ const runtimeDerivedEmptyPassword = await crypto . subtle . importKey ( 'raw' , password , { name : 'PBKDF2' } , false , [ 'deriveKey' ] )
87
+ cryptoKey = await crypto . subtle . deriveKey ( deriveParams , runtimeDerivedEmptyPassword , { name : algorithm , length : keyLength } , true , [ 'decrypt' ] )
88
+ } catch {
89
+ cryptoKey = await crypto . subtle . importKey ( 'jwk' , derivedEmptyPasswordKey , { name : 'AES-GCM' } , true , [ 'decrypt' ] )
90
+ }
82
91
} else {
83
92
// Derive the key using PBKDF2.
84
93
const deriveParams = { name : 'PBKDF2' , salt, iterations, hash : { name : digest } }
0 commit comments