Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
description = "DevOps Boot Common"

dependencies {
api("com.tencent.bk.sdk:crypto-java-sdk")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.devops.common.crypto

import com.tencent.bk.sdk.crypto.util.CipherInputStream
import com.tencent.bk.sdk.crypto.util.CipherOutputStream
import java.io.InputStream
import java.io.OutputStream

/**
* 抽象加密工厂
* */
abstract class AbstractCipherFactory {
/**
* 获取加密的输出流,将写入的数据加密后,写入到[outputStream]
*
* @param outputStream 输出
* @param key 密钥
* @return 密文输出流
* */
abstract fun getEncryptOutputStream(outputStream: OutputStream, key: String): CipherOutputStream

/**
* 获取一个明文输入流,读取[inputStream]中的加密数据,并将其解密,转换成新的输入流
*
* @param inputStream 密文输入流
* @param key 密钥
* @return 明文输入流
* */
abstract fun getPlainInputStream(inputStream: InputStream, key: String): CipherInputStream

/**
* 获取一个加密的输入流,读取[inputStream]中的数据,并将其加密,转换成新的输入流
*
* @param inputStream 明文输入流
* @param key 密钥
* @return 密文输入流
* */
abstract fun getEncryptInputStream(inputStream: InputStream, key: String): EncryptInputStream
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.devops.common.crypto

/**
* 加密工厂生产者,负责生产各类算法的加密工厂
* */
object CipherFactoryProducer {
private val mapping = mutableMapOf<String, AbstractCipherFactory>()

init {
register("SM4", SM4Factory())
}

/**
* 获取工厂
* @param algorithm 加密算法
* @return 加密工厂
* */
fun getFactory(algorithm: String): AbstractCipherFactory {
return mapping[algorithm] ?: throw IllegalArgumentException("$algorithm not supported.")
}

/**
* 注册工厂
* @param algorithm 加密算法
* @param factory 加密工厂
* */
fun register(algorithm: String, factory: AbstractCipherFactory) {
mapping[algorithm] = factory
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.devops.common.crypto

import com.tencent.bk.sdk.crypto.util.CipherInputStream
import java.io.InputStream
import javax.crypto.Cipher
import kotlin.math.min

/**
* 加密输入流,根据指定的加密器,对数据进行加密转换
* */
open class EncryptInputStream(inputStream: InputStream, cipher: Cipher, size: Int) : CipherInputStream(
inputStream,
cipher,
size
) {
private val iv = cipher.iv
private var writeIvPos = 0
override fun read(b: ByteArray, off: Int, len: Int): Int {
if (writeIvPos < iv.size) {
val size = min(len, iv.size - writeIvPos)
iv.copyInto(b, off, writeIvPos, writeIvPos + size)
writeIvPos += size
return size
}
return super.read(b, off, len)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.devops.common.crypto

import com.tencent.bk.sdk.crypto.util.SM4Util
import java.io.InputStream

/**
* SM4加密流,提供将明文输入流,转成成加密输入流
* */
class SM4EncryptInputStream(inputStream: InputStream, key: String) : EncryptInputStream(
inputStream,
SM4Util.creatEncryptCipher(key),
DEFAULT_BUFFER_SIZE
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.devops.common.crypto

import com.tencent.bk.sdk.crypto.util.CipherInputStream
import com.tencent.bk.sdk.crypto.util.CipherOutputStream
import com.tencent.bk.sdk.crypto.util.SM4InputStream
import com.tencent.bk.sdk.crypto.util.SM4OutputStream
import java.io.InputStream
import java.io.OutputStream

/**
* SM4加密工厂
* */
class SM4Factory : AbstractCipherFactory() {
override fun getEncryptOutputStream(outputStream: OutputStream, key: String): CipherOutputStream {
return SM4OutputStream(outputStream, key)
}

override fun getPlainInputStream(inputStream: InputStream, key: String): CipherInputStream {
return SM4InputStream(inputStream, key)
}

override fun getEncryptInputStream(inputStream: InputStream, key: String): EncryptInputStream {
return SM4EncryptInputStream(inputStream, key)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.tencent.devops.common.crypto

import com.tencent.bk.sdk.crypto.util.SM4Util
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import kotlin.random.Random

class CryptoTest {
@Test
fun sm4() {
val plainBytes = Random.nextBytes(10)
val key = "key"
val sM4Factory = CipherFactoryProducer.getFactory("SM4")
// 加密
val encryptInputStream = sM4Factory.getEncryptInputStream(ByteArrayInputStream(plainBytes), key)
val cryptoOutputStream = ByteArrayOutputStream()
encryptInputStream.copyTo(cryptoOutputStream)

// 解密
val plainInputStream =
sM4Factory.getPlainInputStream(ByteArrayInputStream(cryptoOutputStream.toByteArray()), key)
val plainOutputStream = ByteArrayOutputStream()
plainInputStream.copyTo(plainOutputStream)

Assertions.assertArrayEquals(plainBytes, plainOutputStream.toByteArray())

// output加密
val encryptOutputStream = ByteArrayOutputStream()
sM4Factory.getEncryptOutputStream(encryptOutputStream, key).use {
ByteArrayInputStream(plainBytes).copyTo(it)
}
val decryptBytes = SM4Util.decrypt(key.toByteArray(), encryptOutputStream.toByteArray())
Assertions.assertArrayEquals(plainBytes, decryptBytes)
}
}
3 changes: 3 additions & 0 deletions devops-boot-project/devops-boot-dependencies/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@ dependencies {

// s3
api("com.amazonaws:aws-java-sdk-s3:1.11.700")

// crypto
api("com.tencent.bk.sdk:crypto-java-sdk:1.1.1")
}
}