1 /* <lambda>null2 * Copyright (C) 2020 Square, Inc. and others. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package okio 17 18 import javax.crypto.spec.IvParameterSpec 19 import javax.crypto.spec.SecretKeySpec 20 import kotlin.random.Random 21 22 data class CipherAlgorithm( 23 val transformation: String, 24 val padding: Boolean, 25 val keyLength: Int, 26 val ivLength: Int? = null, 27 ) { 28 fun createCipherFactory(random: Random): CipherFactory { 29 val key = random.nextBytes(keyLength) 30 val secretKeySpec = SecretKeySpec(key, transformation.substringBefore('/')) 31 return if (ivLength == null) { 32 CipherFactory(transformation) { mode -> 33 init(mode, secretKeySpec) 34 } 35 } else { 36 val iv = random.nextBytes(ivLength) 37 val ivParameterSpec = IvParameterSpec(iv) 38 CipherFactory(transformation) { mode -> 39 init(mode, secretKeySpec, ivParameterSpec) 40 } 41 } 42 } 43 44 override fun toString() = transformation 45 46 companion object { 47 val BLOCK_CIPHER_ALGORITHMS 48 get() = listOf( 49 CipherAlgorithm("AES/CBC/NoPadding", false, 16, 16), 50 CipherAlgorithm("AES/CBC/PKCS5Padding", true, 16, 16), 51 CipherAlgorithm("AES/ECB/NoPadding", false, 16), 52 CipherAlgorithm("AES/ECB/PKCS5Padding", true, 16), 53 CipherAlgorithm("DES/CBC/NoPadding", false, 8, 8), 54 CipherAlgorithm("DES/CBC/PKCS5Padding", true, 8, 8), 55 CipherAlgorithm("DES/ECB/NoPadding", false, 8), 56 CipherAlgorithm("DES/ECB/PKCS5Padding", true, 8), 57 CipherAlgorithm("DESede/CBC/NoPadding", false, 24, 8), 58 CipherAlgorithm("DESede/CBC/PKCS5Padding", true, 24, 8), 59 CipherAlgorithm("DESede/ECB/NoPadding", false, 24), 60 CipherAlgorithm("DESede/ECB/PKCS5Padding", true, 24), 61 ) 62 } 63 } 64