为avid-bvid转换和Wbi签名添加Swift实现 (#890)
* Update bvid_desc.md * Update wbi.md
This commit is contained in:
@@ -192,6 +192,65 @@ print(bv2av("BV1L9Uoa9EUx"))
|
||||
|
||||
参考 <https://github.com/Colerar/abv/blob/main/src/lib.rs>
|
||||
|
||||
### Swift
|
||||
|
||||
```swift
|
||||
fileprivate let XOR_CODE: UInt64 = 23442827791579
|
||||
fileprivate let MASK_CODE: UInt64 = 2251799813685247
|
||||
fileprivate let MAX_AID: UInt64 = 1 << 51
|
||||
|
||||
fileprivate let data: [UInt8] = [70, 99, 119, 65, 80, 78, 75, 84, 77, 117, 103, 51, 71, 86, 53, 76, 106, 55, 69, 74, 110, 72, 112, 87, 115, 120, 52, 116, 98, 56, 104, 97, 89, 101, 118, 105, 113, 66, 122, 54, 114, 107, 67, 121, 49, 50, 109, 85, 83, 68, 81, 88, 57, 82, 100, 111, 90, 102]
|
||||
|
||||
fileprivate let BASE: UInt64 = 58
|
||||
fileprivate let BV_LEN: Int = 12
|
||||
fileprivate let PREFIX: String = "BV1"
|
||||
|
||||
func av2bv(avid: UInt64) -> String {
|
||||
var bytes: [UInt8] = [66, 86, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48]
|
||||
var bvIdx = BV_LEN - 1
|
||||
var tmp = (MAX_AID | avid) ^ XOR_CODE
|
||||
|
||||
while tmp != 0 {
|
||||
bytes[bvIdx] = data[Int(tmp % BASE)]
|
||||
tmp /= BASE
|
||||
bvIdx -= 1
|
||||
}
|
||||
|
||||
bytes.swapAt(3, 9)
|
||||
bytes.swapAt(4, 7)
|
||||
|
||||
return String(decoding: bytes, as: UTF8.self)
|
||||
}
|
||||
|
||||
func bv2av(bvid: String) -> UInt64 {
|
||||
let fixedBvid: String
|
||||
if bvid.hasPrefix("BV") {
|
||||
fixedBvid = bvid
|
||||
} else {
|
||||
fixedBvid = "BV" + bvid
|
||||
}
|
||||
var bvidArray = Array(fixedBvid.utf8)
|
||||
|
||||
bvidArray.swapAt(3, 9)
|
||||
bvidArray.swapAt(4, 7)
|
||||
|
||||
let trimmedBvid = String(decoding: bvidArray[3...], as: UTF8.self)
|
||||
|
||||
var tmp: UInt64 = 0
|
||||
|
||||
for char in trimmedBvid {
|
||||
if let idx = data.firstIndex(of: char.utf8.first!) {
|
||||
tmp = tmp * BASE + UInt64(idx)
|
||||
}
|
||||
}
|
||||
|
||||
return (tmp & MASK_CODE) ^ XOR_CODE
|
||||
}
|
||||
|
||||
print(av2bv(avid: 111298867365120))
|
||||
print(bv2av(bvid: "BV1L9Uoa9EUx"))
|
||||
```
|
||||
|
||||
## 老版算法存档
|
||||
|
||||
算法参考自[【揭秘】av号转bv号的过程](https://www.bilibili.com/video/BV1N741127Tj)
|
||||
|
||||
Reference in New Issue
Block a user