This repository has been archived by the owner on Dec 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
86 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) Richasy. All rights reserved. | ||
|
||
#pragma warning disable SA1300 // Element should begin with upper-case letter | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace Bili.Models.gRPC | ||
{ | ||
/// <summary> | ||
/// BUVID. | ||
/// </summary> | ||
public class Buvid | ||
{ | ||
private readonly string _mac; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Buvid"/> class. | ||
/// </summary> | ||
/// <param name="macAddress">MAC 地址.</param> | ||
public Buvid(string macAddress) => _mac = macAddress; | ||
|
||
/// <summary> | ||
/// 生成 buvid. | ||
/// </summary> | ||
/// <returns>buvid.</returns> | ||
public string Generate() | ||
{ | ||
var buvidPrefix = "XY"; | ||
var inputStrMd5 = GetMd5Hash(_mac.Replace(":", string.Empty)); | ||
|
||
var buvidRaw = new StringBuilder(); | ||
buvidRaw.Append(buvidPrefix); | ||
buvidRaw.Append(inputStrMd5[2]); | ||
buvidRaw.Append(inputStrMd5[12]); | ||
buvidRaw.Append(inputStrMd5[22]); | ||
buvidRaw.Append(inputStrMd5); | ||
|
||
return buvidRaw.ToString(); | ||
} | ||
|
||
private static string GetMd5Hash(string input) | ||
{ | ||
using (var md5 = MD5.Create()) | ||
{ | ||
var inputBytes = Encoding.UTF8.GetBytes(input); | ||
var hashBytes = md5.ComputeHash(inputBytes); | ||
var sb = new StringBuilder(); | ||
foreach (var b in hashBytes) | ||
{ | ||
sb.Append(b.ToString("x2")); | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
} | ||
} | ||
} |