parent
e0588d130e
commit
d0e58ae674
@ -0,0 +1,54 @@ |
||||
/* |
||||
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved. |
||||
* 项目名称:C++ 信息安全性设计准则 |
||||
* 项目描述:用于检查C++源代码的安全性设计准则的Sonarqube插件 |
||||
* 版权说明:本软件属北京关键科技股份有限公司所有,在未获得北京关键科技股份有限公司正式授权情况下,任何企业和个人,不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。 |
||||
*/ |
||||
package com.keyware.sonar.cxx.rules.checkers; |
||||
|
||||
import com.sonar.cxx.sslr.api.AstNode; |
||||
import com.sonar.cxx.sslr.api.Grammar; |
||||
import org.sonar.check.Priority; |
||||
import org.sonar.check.Rule; |
||||
import org.sonar.cxx.parser.CxxGrammarImpl; |
||||
import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault; |
||||
import org.sonar.cxx.squidbridge.annotations.SqaleConstantRemediation; |
||||
import org.sonar.cxx.squidbridge.checks.SquidCheck; |
||||
|
||||
import javax.annotation.Nullable; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* TODO HighEncryptDES |
||||
* |
||||
* @author RenFengJiang |
||||
* @date 2024/1/15 |
||||
*/ |
||||
|
||||
@Rule(key = "FVNRShaChecker", name = "应使用不可逆标准散列算法", description = "采用当前公认的不可逆标准散列算法", priority = Priority.INFO, tags = {"28suo"}) |
||||
@ActivatedByDefault |
||||
@SqaleConstantRemediation("5min") |
||||
public class FVNRShaChecker extends SquidCheck<Grammar> { |
||||
@Override |
||||
public void init() { |
||||
// 订阅要检查AST节点类型,用于在visitNode方法中检查该类型节点
|
||||
this.subscribeTo( |
||||
CxxGrammarImpl.translationUnit |
||||
); |
||||
} |
||||
|
||||
|
||||
public void visitFile(@Nullable AstNode astNode) { |
||||
//逐行获取内容
|
||||
List<String> inputFileLines = getContext().getInputFileLines(); |
||||
for (String str:inputFileLines) { |
||||
if(str.startsWith("#include")){ |
||||
if(str.contains("<openssl/") || str.contains("<cryptopp/") ){ |
||||
if(!str.contains("<openssl/sha.h>") && !str.contains("<cryptopp/blake2.h>") && !str.contains("<cryptopp/md5.h>")){ |
||||
getContext().createFileViolation(this, "应使用不可逆标准散列算法"); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,32 @@ |
||||
/* |
||||
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved. |
||||
* 项目名称:C++ 信息安全性设计准则 |
||||
* 项目描述:用于检查C++源代码的安全性设计准则的Sonarqube插件 |
||||
* 版权说明:本软件属北京关键科技股份有限公司所有,在未获得北京关键科技股份有限公司正式授权情况下,任何企业和个人,不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。 |
||||
*/ |
||||
package com.keyware.sonar.cxx.rules.checkers; |
||||
|
||||
import com.keyware.sonar.cxx.CxxFileTesterHelper; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.sonar.cxx.CxxAstScanner; |
||||
import org.sonar.cxx.squidbridge.api.SourceFile; |
||||
import org.sonar.cxx.squidbridge.checks.CheckMessagesVerifier; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
/** |
||||
* TODO HighEncryptDESTest |
||||
* |
||||
* @author RenFengJiang |
||||
* @date 2024/1/15 |
||||
*/ |
||||
public class FVNRShaCheckerTest { |
||||
@Test |
||||
public void checkTest() throws IOException { |
||||
var checker = new FVNRShaChecker(); |
||||
var tester = CxxFileTesterHelper.create("FVNRShaChecker.cc"); |
||||
SourceFile file = CxxAstScanner.scanSingleInputFile(tester.asInputFile(), checker); |
||||
CheckMessagesVerifier.verify(file.getCheckMessages()) |
||||
.next().withMessage("应使用不可逆标准散列算法"); |
||||
} |
||||
} |
@ -0,0 +1,35 @@ |
||||
#include <openssl/sha.h> |
||||
#include <cryptopp/md5.h> |
||||
#include <cryptopp/blake2.h> |
||||
#include <openssl/aes.h>// error |
||||
#include <string> |
||||
#include <iostream> |
||||
|
||||
// 函数:计算给定字符串的SHA-256哈希值
|
||||
std::string calculateSHA256(const std::string& input) { |
||||
unsigned char hash[SHA256_DIGEST_LENGTH]; // SHA-256哈希值长度为32字节(256位)
|
||||
|
||||
SHA256_CTX sha256; |
||||
SHA256_Init(&sha256); // 初始化上下文
|
||||
|
||||
SHA256_Update(&sha256, input.c_str(), input.size()); // 将输入数据添加到上下文中
|
||||
|
||||
SHA256_Final(hash, &sha256); // 计算哈希值并存储在hash数组中
|
||||
|
||||
// 转换为十六进制字符串形式以便输出或保存
|
||||
std::string output; |
||||
for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) { |
||||
char hex[3] = {0}; |
||||
sprintf(hex, "%02x", hash[i]); |
||||
output += hex; |
||||
} |
||||
|
||||
return output; |
||||
} |
||||
|
||||
int main() { |
||||
std::string data = "This is a test string to be hashed with SHA-256."; |
||||
std::cout << "SHA-256 Hash: " << calculateSHA256(data) << std::endl; |
||||
|
||||
return 0; |
||||
} |
Loading…
Reference in new issue