提交 1188523e 编写于 作者: B baiy 提交者: ninecents

支持国密算法 sm2 sm3 #23

上级 a41f3847
......@@ -11,8 +11,8 @@
## 功能列表
|功能|说明|离线使用|
|---|---|---|
|哈希|`md5`, `sha1`, `sha256`, `sha512`|√|
|加密/解密|`AES`,`DES`,`RC4`,`Rabbit`,`TripleDes`|√|
|哈希|`md5`, `sha1`, `sha256`, `sha512`,`sm3`|√|
|加密/解密|`AES`,`DES`,`RC4`,`Rabbit`,`TripleDes`,`sm2`|√|
|BASE64编码|`加密`,`解密`|√|
|URL编码|`编码`,`解码`|√|
|时间戳|双向转换|√|
......@@ -63,6 +63,7 @@
- [pconline](http://whois.pconline.com.cn/)
- [moment](https://momentjs.com/)
- [vue-codemirror](https://www.npmjs.com/package/vue-codemirror)
- [sm-crypto](https://github.com/JuneAndGreen/sm-crypto)
> 当然项目中还使用很多不知道姓名的大神的代码, 在这里就不一一感谢
......
......@@ -11540,6 +11540,21 @@
"is-fullwidth-code-point": "^2.0.0"
}
},
"sm-crypto": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/sm-crypto/-/sm-crypto-0.1.4.tgz",
"integrity": "sha512-zAu1lvSZNAqkR3B/gdfI4WRUPCHtr5joFd/jzouz1bEkYtH9pi/44YaYWerjlDZi6llZBoFs7MRPP7YEVtKPjw==",
"requires": {
"jsbn": "^1.1.0"
},
"dependencies": {
"jsbn": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/jsbn/-/jsbn-1.1.0.tgz",
"integrity": "sha1-sBMHyym2GKHtJux56RH4A8TaAEA="
}
}
},
"snapdragon": {
"version": "0.8.2",
"resolved": "http://registry.npm.taobao.org/snapdragon/download/snapdragon-0.8.2.tgz",
......
{
"name": "c-tool",
"version": "1.1.0",
"version": "1.2.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve --port 8081",
......@@ -30,6 +30,7 @@
"query-string": "^6.13.1",
"radix.js": "0.0.1",
"serialize-php": "^1.1.2",
"sm-crypto": "^0.1.4",
"system": "^2.0.1",
"view-design": "^4.3.2",
"vue": "^2.6.12",
......
<template>
<heightResize ignore :append="['.page-option-block']" @resize="resize">
<autoHeightTextarea v-model="current.input" :height="inputHeight" :placeholder="$t('encrypt_input')"/>
<option-block class="page-option-block">
<div>
<Input v-model="current.input" :rows="7" type="textarea" placeholder="内容"></Input>
<option-block>
<FormItem>
<Select v-model="current.type" style="width:200px">
<Option v-for="v in type" :value="v" :key="v">{{ v }}</Option>
</Select>
</FormItem>
<FormItem>
<Input v-model="current.password" :placeholder="$t('encrypt_password')"></Input>
<Input v-model="current.password" placeholder="密码/秘钥"></Input>
</FormItem>
<FormItem v-if="current.type === 'SM2'">
<Select v-model="current.sm2CipherMode" style="width:100px">
......@@ -18,35 +18,25 @@
</FormItem>
<FormItem>
<ButtonGroup>
<Button type="primary" @click="handle('encrypt')">{{ $t('encrypt_encrypt') }}</Button>
<Button type="primary" @click="handle('decrypt')">{{ $t('encrypt_decrypt') }}</Button>
<Button type="primary" @click="sm2Generate()" v-if="current.type === 'SM2'">
{{ $t('encrypt_generate_secret_key') }}
</Button>
<Button type="primary" @click="handle('encrypt')">加密</Button>
<Button type="primary" @click="handle('decrypt')">解密</Button>
<Button type="primary" @click="sm2Generate()" v-if="current.type === 'SM2'">生成密钥对</Button>
</ButtonGroup>
</FormItem>
</option-block>
<autoHeightTextarea :value="current.output" :height="outputHeight" :placeholder="$t('encrypt_output')"/>
</heightResize>
<Input v-model="current.output" :rows="7" type="textarea" placeholder="结果"></Input>
</div>
</template>
<script>
import crypto from "crypto-js"
import heightResize from "./components/heightResize";
import autoHeightTextarea from "./components/autoHeightTextarea";
export default {
components: {
heightResize,
autoHeightTextarea
},
created() {
this.$initToolData('input')
},
methods: {
handle(v) {
const sm2 = require('sm-crypto').sm2
if (this.current.input) {
try {
import crypto from "crypto-js"
export default {
created() {
this.current = Object.assign(this.current,this.$getToolData("input"))
},
methods: {
handle(v) {
const sm2 = require('sm-crypto').sm2
if (this.current.input) {
switch (this.current.type) {
case "AES":
case "DES":
......@@ -58,7 +48,8 @@ export default {
this.current.input,
this.current.password
).toString();
} else {
}
else{
this.current.output = crypto[this.current.type].decrypt(
this.current.input,
this.current.password
......@@ -67,13 +58,14 @@ export default {
break;
case "SM2":
if (v === "encrypt") {
this.current.output = sm2.doEncrypt(
this.current.output =sm2.doEncrypt(
this.current.input,
this.current.password,
this.current.sm2CipherMode
);
} else {
this.current.output = sm2.doDecrypt(
}
else{
this.current.output =sm2.doDecrypt(
this.current.input,
this.current.password,
this.current.sm2CipherMode
......@@ -83,59 +75,43 @@ export default {
default:
return;
}
} catch (e) {
return this.$Message.error(
this.$t('encrypt_failed', [e.message]).toString()
)
this.current.operation = v;
this.$clipboardCopy(this.current.output);
this.$saveToolData(this.current);
}
this.current.operation = v;
this.$clipboardCopy(this.current.output);
this.$saveToolData(this.current);
}
},
sm2Generate(){
const sm2 = require('sm-crypto').sm2
let keypair = sm2.generateKeyPairHex()
let string = "公钥:\n"+keypair.publicKey+"\n"+"私钥:\n"+keypair.privateKey+"\n\n"+
"请及时保存秘钥对, 关闭对话框后无法恢复当前秘钥数据"
this.$Modal.info({
render: (h) => {
return h('Input', {
props: {
value: string,
type:"textarea",
rows:9
}
})
},
okText:"关闭",
width:600
})
},
},
sm2Generate() {
const sm2 = require('sm-crypto').sm2
let keypair = sm2.generateKeyPairHex()
let string = [
this.$t('encrypt_public_key'),
keypair.publicKey,
this.$t('encrypt_private_key'),
keypair.privateKey, '',
this.$t('encrypt_secret_key_prompt')
].join("\n");
this.$Modal.info({
render: (h) => {
return h('Input', {
props: {
value: string,
type: "textarea",
rows: 9
}
})
data() {
return {
current:{
input: "",
password:"",
sm2CipherMode:"C1C3C2",
output: "",
type:"AES",
operation:""
},
okText: this.$t('encrypt_close'),
width: 600
})
type: ["AES","DES","RC4","Rabbit","TripleDes","SM2"],
}
},
resize(height) {
this.inputHeight = Math.min(160, Math.ceil(height / 2))
this.outputHeight = height - this.inputHeight
}
},
data() {
return {
current: {
input: "",
password: "",
sm2CipherMode: "C1C3C2",
output: "",
type: "AES",
operation: ""
},
type: ["AES", "DES", "RC4", "Rabbit", "TripleDes", "SM2"],
inputHeight: 100,
outputHeight: 100
}
},
}
</script>
}
</script>
\ No newline at end of file
<template>
<div id="tool-hash">
<Row :gutter="10">
<Col span="8">
<input-block>
<Input v-model="current.input" :rows="18" type="textarea" :placeholder="$t('hash_content')"></Input>
<Checkbox slot="extra" v-model="current.isUppercase">{{ $t('hash_uppercase') }}</Checkbox>
</input-block>
</Col>
<Col span="16">
<input-block style="margin-bottom: 6px" :text="type" v-for="type in types" :key="type" @on-default-right-bottom-click="()=>copy(type)">
<Input :value="result(type)" :placeholder="type" :rows="3" type="textarea"></Input>
</input-block>
</Col>
</Row>
<div>
<Input v-model="current.input" :rows="7" type="textarea" placeholder="内容"></Input>
<option-block>
<FormItem>
<ButtonGroup>
<Button type="primary" @click="handle(v)" v-for="v in type" :key="v">{{v}}</Button>
</ButtonGroup>
</FormItem>
</option-block>
<Input v-model="current.output" :rows="7" type="textarea" placeholder="结果"></Input>
</div>
</template>
<script>
import crypto from "crypto-js"
const sm = require('sm-crypto');
export default {
created() {
this.$initToolData('input')
},
computed: {
md5() {
let result = crypto.MD5(this.current.input).toString();
return this.current.isUppercase ? result.toUpperCase() : result;
import crypto from "crypto-js"
export default {
created() {
this.current = Object.assign(this.current,this.$getToolData("input"))
},
sha1() {
let result = crypto.SHA1(this.current.input).toString();
return this.current.isUppercase ? result.toUpperCase() : result;
},
sha256() {
let result = crypto.SHA256(this.current.input).toString();
return this.current.isUppercase ? result.toUpperCase() : result;
},
sha512() {
let result = crypto.SHA512(this.current.input).toString();
return this.current.isUppercase ? result.toUpperCase() : result;
},
sm3() {
let result = sm.sm3(this.current.input);
return this.current.isUppercase ? result.toUpperCase() : result;
},
},
watch: {
current: {
handler() {
if (this.current.input){
methods: {
handle(v) {
if (this.current.input) {
switch (v) {
case "md5":
this.current.output = crypto.MD5(this.current.input).toString();
break;
case "sha1":
this.current.output = crypto.SHA1(this.current.input).toString();
break;
case "sha256":
this.current.output = crypto.SHA256(this.current.input).toString();
break;
case "sha512":
this.current.output = crypto.SHA512(this.current.input).toString();
break;
case "sm3":
this.current.output = require('sm-crypto').sm3(this.current.input);
break;
default:
return;
}
this.current.operation = v;
this.$clipboardCopy(this.current.output);
this.$saveToolData(this.current);
}
},
deep: true
}
},
methods:{
result(type){
if (!this.current.input) {
return "";
}
return this[type]
},
copy(type){
if (this[type]){
this.$clipboardCopy(this[type])
data() {
return {
current:{
input: "",
output: "",
operation:""
},
type: ['md5', 'sha1', 'sha256', 'sha512',"sm3"]
}
}
},
data() {
return {
current: {
input: "",
isUppercase: false,
},
types: ['md5', 'sha1', 'sha256', 'sha512', "sm3"]
}
},
}
</script>
},
}
</script>
\ No newline at end of file
<template>
<div>11</div>
</template>
<script>
export default {
name: "smCrypto",
created () {
const sm2 = require('sm-crypto').sm2
console.log(sm2.generateKeyPairHex())
}
}
</script>
<style scoped>
</style>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册