Skip to content

Commit

Permalink
fix: clear hash
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohaodu committed May 21, 2024
1 parent 6b481ff commit e400d33
Show file tree
Hide file tree
Showing 8 changed files with 7 additions and 112 deletions.
1 change: 0 additions & 1 deletion packages/web-chat-x-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"element-plus": "^2.6.1",
"emoji-picker-element": "^1.21.3",
"emoji-regex": "^10.3.0",
"multiformats": "^13.1.0",
"peerjs": "^1.5.2",
"qrcode": "^1.5.3",
"typeson": "^9.0.3",
Expand Down
17 changes: 1 addition & 16 deletions packages/web-chat-x-vue/src/classes/ChatChannel.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
generateAndSetIdentifier,
getPeerIdFromUserId,
// hashPassword,
} from "@/utils";
import { generateAndSetIdentifier, getPeerIdFromUserId } from "@/utils";
import { PeerId } from "@libp2p/interface";
import { peerIdFromString } from "@libp2p/peer-id";
export default class ChatChannel implements ChatChannelInfo {
Expand All @@ -15,8 +11,6 @@ export default class ChatChannel implements ChatChannelInfo {
friendIds: string[],
channelIds: string[],
relayMultiaddrs: string[],
password: string,
hashedPassword: string,
description: string,
unique?: string
) {
Expand All @@ -25,8 +19,6 @@ export default class ChatChannel implements ChatChannelInfo {
this.userId = userId;
this.peerId = peerId;
this.messagesId = messagesId;
this.password = password;
this.hashedPassword = hashedPassword;
this.description = description;
this.friendIds = [...friendIds];
this.channelIds = [...channelIds];
Expand All @@ -52,16 +44,13 @@ export default class ChatChannel implements ChatChannelInfo {
phone: string;
location: string;
role: string;
password: string;
hashedPassword: string;
relayMultiaddrs: string[];
stunTurn: string[];
friendIds: string[];
channelIds: string[];
messagesId: string;
static async create(
name: string,
password: string,
description: string,
friendIds: string[],
channelIds: string[],
Expand All @@ -83,8 +72,6 @@ export default class ChatChannel implements ChatChannelInfo {
friendIds,
channelIds,
relayMultiaddrs,
password,
password,
description,
unique
);
Expand All @@ -99,8 +86,6 @@ export default class ChatChannel implements ChatChannelInfo {
friendIds,
channelIds,
relayMultiaddrs,
password,
password,
description,
unique
);
Expand Down
17 changes: 1 addition & 16 deletions packages/web-chat-x-vue/src/classes/ChatUser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
getPeerIdFromUserId,
// hashPassword,
generateAndSetIdentifier,
} from "@/utils";
import { getPeerIdFromUserId, generateAndSetIdentifier } from "@/utils";
import { PeerId } from "@libp2p/interface";
import { peerIdFromString } from "@libp2p/peer-id";
export default class ChatUser implements ChatUserInfo {
Expand All @@ -15,8 +11,6 @@ export default class ChatUser implements ChatUserInfo {
friendIds: string[],
channelIds: string[],
relayMultiaddrs: string[],
password: string,
hashedPassword: string,
description: string,
unique?: string
) {
Expand All @@ -25,8 +19,6 @@ export default class ChatUser implements ChatUserInfo {
this.userId = userId;
this.peerId = peerId;
this.messagesId = messagesId;
this.password = password;
this.hashedPassword = hashedPassword;
this.description = description;
this.friendIds = [...friendIds];
this.channelIds = [...channelIds];
Expand All @@ -53,16 +45,13 @@ export default class ChatUser implements ChatUserInfo {
phone: string;
location: string;
role: string;
password: string;
hashedPassword: string;
relayMultiaddrs: string[];
stunTurn: string[];
friendIds: string[];
channelIds: string[];
messagesId: string;
static async create(
name: string,
password: string,
description: string,
friendIds: string[],
channelIds: string[],
Expand All @@ -84,8 +73,6 @@ export default class ChatUser implements ChatUserInfo {
friendIds,
channelIds,
relayMultiaddrs,
password,
password,
description,
unique
);
Expand All @@ -100,8 +87,6 @@ export default class ChatUser implements ChatUserInfo {
friendIds,
channelIds,
relayMultiaddrs,
password,
password,
description,
unique
);
Expand Down
7 changes: 0 additions & 7 deletions packages/web-chat-x-vue/src/classes/Libp2pManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,12 @@ export class Libp2pManager {
) {
const channel = await ChatChannel.create(
channelName,
"",
channelDescription,
[],
[],
[],
"",
"",
"",
channelId
);
(this.libp2p?.services.pubsub as PubSub<GossipsubEvents>).subscribe(
Expand All @@ -403,15 +401,13 @@ export class Libp2pManager {
subscriber: PeerId
): Promise<void> => {
const newSubscriber = await ChatUser.create(
"",
"",
"",
[],
[],
[],
"",
"",
"",
subscriber.toString()
);
await this.addChannelSubscriber(true, channel, newSubscriber);
Expand Down Expand Up @@ -557,7 +553,6 @@ export class Libp2pManager {
]);
const friend = await ChatUser.create(
remoteMessageObject.user.name,
"",
remoteMessageObject.user.description,
[],
[],
Expand Down Expand Up @@ -640,7 +635,6 @@ export class Libp2pManager {
console.log(topic, pubsubSendMessage, signedMessage);
const channelSubscribe = await ChatUser.create(
pubsubSendMessage.user.name,
"",
pubsubSendMessage.user.description,
[],
[],
Expand Down Expand Up @@ -851,7 +845,6 @@ export class Libp2pManager {
const friend = await ChatUser.create(
responseObject.user.name,
"",
"",
[],
[],
[],
Expand Down
2 changes: 0 additions & 2 deletions packages/web-chat-x-vue/src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ declare global {
phone: string; // 联系电话
location: string; // 地址
role: string; // 身份
password: string;
hashedPassword: string; // 哈希加密后的密码
isOnline: boolean;
}

Expand Down
44 changes: 4 additions & 40 deletions packages/web-chat-x-vue/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ import crc32 from "crc/crc32";
import { peerIdFromKeys, peerIdFromString } from "@libp2p/peer-id";
import { PeerId } from "@libp2p/interface";
import { generateKeyPair } from "@libp2p/crypto/keys";
import { sha256 } from "multiformats/hashes/sha2";
import { toHex } from "multiformats/bytes";

/**
* 生成一个Ed25519类型的PeerId。
* 使用`@libp2p/crypto`生成密钥对,并通过公钥创建PeerId。
Expand All @@ -95,24 +92,6 @@ async function generatePeerId(): Promise<PeerId> {
const peerId = await peerIdFromKeys(keyPair.public.bytes, keyPair.bytes);
return peerId;
}

/**
* 计算给定密钥(字符串或Uint8Array)的SHA-256哈希,并将其转换为十六进制字符串。
*/
async function getKeyHash(key: string | Uint8Array): Promise<string> {
let keyUint8Array: Uint8Array;

if (key instanceof Uint8Array) {
keyUint8Array = key;
} else {
// 使用 TextEncoder 将字符串编码为 Uint8Array
const encoder = new TextEncoder();
keyUint8Array = encoder.encode(key);
}
// 使用 @libp2p/crypto 提供的 sha256 方法计算哈希
const hash = await sha256.encode(keyUint8Array);
return toHex(hash);
}
/**
* 生成指定长度的随机十六进制字符字符串,用于混淆代码等场景。
*/
Expand All @@ -131,19 +110,11 @@ export function generateRandomHexChars(length = 8): string {
*/
export async function buildUserId(
peerId: string,
obfuscationCode: string,
hash = false
obfuscationCode: string
): Promise<string> {
if (hash) {
const peerIdHash = await getKeyHash(peerId);
const combined = peerIdHash + obfuscationCode;
const crcChecksum = crc32(combined).toString(16);
return combined + crcChecksum;
} else {
const combined = peerId + obfuscationCode;
const crcChecksum = crc32(combined).toString(16);
return combined + crcChecksum;
}
const combined = peerId + obfuscationCode;
const crcChecksum = crc32(combined).toString(16);
return combined + crcChecksum;
}

/**
Expand Down Expand Up @@ -223,13 +194,6 @@ export function getPeerIdFromUserId(userId: string): string {
return peerId;
}

/**
* 异步函数,计算并返回密码的SHA-256哈希值。
*/
export async function hashPassword(_password: string): Promise<string> {
return "";
// return await getKeyHash(password);
}
/**
* 生成或设置标识符(PeerId、混淆代码、用户ID)的方法。
* 可以接受外部提供的PeerId字符串,或自动生成,同时创建混淆代码和用户ID。
Expand Down
28 changes: 1 addition & 27 deletions packages/web-chat-x-vue/src/views/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,6 @@
</template>
</el-input>
</el-form-item>
<!-- <el-form-item label="密码" prop="password">
<el-input
type="password"
autocomplete="new-password"
v-model="form.password"
placeholder="密码"
show-password
>
<template #prefix>
<el-icon>
<el-icon-lock />
</el-icon>
</template>
</el-input>
</el-form-item> -->
</div>
</transition>

Expand Down Expand Up @@ -143,7 +128,6 @@ const currentUser =
const form = reactive({
loginType: currentUser.value ? "local" : "new",
name: "",
password: "",
});
const rules = computed(() => {
Expand All @@ -153,7 +137,6 @@ const rules = computed(() => {
{ required: true, message: "请选择登录方式", trigger: "change" },
],
name: [{ required: true, message: "请输入昵称", trigger: "blur" }],
// password: [{ required: true, message: "请输入密码", trigger: "blur" }],
});
}
});
Expand Down Expand Up @@ -185,15 +168,7 @@ async function submitForm() {
});
if (form.loginType === "new") {
// 存储新用户的哈希密码
const chatUser = await ChatUser.create(
form.name,
form.password,
"",
[],
[],
[],
""
);
const chatUser = await ChatUser.create(form.name, "", [], [], [], "");
await libp2pManager.createNewUser(chatUser);
await libp2pManager.createActivateUserDb(chatUser, chatUser.id);
await libp2pManager.createLibp2pNode(chatUser.peerId);
Expand Down Expand Up @@ -234,7 +209,6 @@ async function submitForm() {
router.push({ name: "Homepage" });
// 清空表单及临时变量
form.name = "";
form.password = "";
} else {
console.log("valid ", valid, "error submit!");
}
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e400d33

Please sign in to comment.