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

支持国密算法 sm2 sm3 #23

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