1- using System ;
2- using System . Collections . Generic ;
3- using System . Linq ;
1+ using System . Globalization ;
42using System . Text ;
5- using System . Threading . Tasks ;
63using M3u8Downloader_H . Abstractions . M3u8 ;
74using M3u8Downloader_H . Common . Extensions ;
85
96namespace M3u8Downloader_H . Common . M3u8
107{
118 public class M3uKeyInfoHelper ( string method , byte [ ] bytes , byte [ ] iv ) : IM3uKeyInfo
129 {
10+ private static readonly Dictionary < string , ( int , int ) > KeyGroup = new ( ) { { "AES-128" , ( 16 , 24 ) } , { "AES-192" , ( 24 , 32 ) } , { "AES-256" , ( 32 , 44 ) } } ;
11+
1312 public string Method { get ; } = method ;
1413
1514 public Uri Uri { get ; } = default ! ;
@@ -20,24 +19,57 @@ public class M3uKeyInfoHelper(string method, byte[] bytes, byte[] iv) : IM3uKeyI
2019
2120 public static IM3uKeyInfo GetKeyInfoInstance ( string method , byte [ ] bKey , byte [ ] ? iv )
2221 {
23- byte [ ] data = bKey . TryParseKey ( method ) ;
22+ byte [ ] data = TryParseKey ( method , bKey ) ;
2423 return new M3uKeyInfoHelper ( method , data , iv ! ) ;
2524 }
2625
2726 public static IM3uKeyInfo GetKeyInfoInstance ( IM3uKeyInfo m3UKeyInfo )
2827 {
29- byte [ ] data = m3UKeyInfo . BKey . TryParseKey ( m3UKeyInfo . Method ) ;
28+ byte [ ] data = TryParseKey ( m3UKeyInfo . Method , m3UKeyInfo . BKey ) ;
3029 return new M3uKeyInfoHelper ( m3UKeyInfo . Method , data , m3UKeyInfo . IV ) ;
3130 }
3231
3332 public static IM3uKeyInfo GetKeyInfoInstance ( string method , string key )
3433 {
35- return new M3uKeyInfoHelper ( method , Encoding . UTF8 . GetBytes ( key ) , null ! ) ;
34+ byte [ ] data = TryParseKey ( method , key ) ;
35+ return new M3uKeyInfoHelper ( method , data , null ! ) ;
3636 }
3737
3838 public static IM3uKeyInfo GetKeyInfoInstance ( string method , string key , string iv )
3939 {
40- return new M3uKeyInfoHelper ( method , Encoding . UTF8 . GetBytes ( key ) , iv ? . ToHex ( ) ! ) ;
40+ byte [ ] data = TryParseKey ( method , key ) ;
41+ return new M3uKeyInfoHelper ( method , data , iv ? . ToHex ( ) ! ) ;
42+ }
43+
44+ private static byte [ ] TryParseKey ( string method , byte [ ] data )
45+ {
46+ string tmpMethod = string . IsNullOrWhiteSpace ( method ) ? "AES-128" : method . ToUpper ( CultureInfo . CurrentCulture ) . Trim ( ) ;
47+ if ( KeyGroup . TryGetValue ( tmpMethod , out ( int , int ) tmpKey ) )
48+ {
49+ if ( data . Length == tmpKey . Item1 )
50+ return data ;
51+ else if ( data . Length == tmpKey . Item2 )
52+ {
53+ var stringdata = Encoding . UTF8 . GetString ( data ) ;
54+ return Convert . FromBase64String ( stringdata ) ;
55+ }
56+ }
57+ throw new InvalidCastException ( "无法解析的密钥,请确定是否为AES-128,AES-192,AES-256" ) ;
58+ }
59+
60+ private static byte [ ] TryParseKey ( string method , string data )
61+ {
62+ string tmpMethod = string . IsNullOrWhiteSpace ( method ) ? "AES-128" : method . ToUpper ( CultureInfo . CurrentCulture ) . Trim ( ) ;
63+ if ( KeyGroup . TryGetValue ( tmpMethod , out ( int , int ) tmpKey ) )
64+ {
65+ if ( data . Length == tmpKey . Item1 )
66+ return Encoding . UTF8 . GetBytes ( data ) ;
67+ else if ( data . Length == tmpKey . Item2 )
68+ {
69+ return Convert . FromBase64String ( data ) ;
70+ }
71+ }
72+ throw new InvalidCastException ( "无法解析的密钥,请确定是否为AES-128,AES-192,AES-256" ) ;
4173 }
4274 }
4375}
0 commit comments