x2m_crypto.go 808 B

1234567891011121314151617181920212223242526272829303132333435
  1. package ximalaya
  2. import (
  3. _ "embed"
  4. "encoding/binary"
  5. )
  6. const x2mHeaderSize = 1024
  7. var x2mKey = [...]byte{'x', 'm', 'l', 'y'}
  8. var x2mScrambleTable = [x2mHeaderSize]uint16{}
  9. //go:embed x2m_scramble_table.bin
  10. var x2mScrambleTableBytes []byte
  11. func init() {
  12. if len(x2mScrambleTableBytes) != 2*x2mHeaderSize {
  13. panic("invalid x2m scramble table")
  14. }
  15. for i := range x2mScrambleTable {
  16. x2mScrambleTable[i] = binary.LittleEndian.Uint16(x2mScrambleTableBytes[i*2:])
  17. }
  18. }
  19. // decryptX2MHeader decrypts the header of ximalaya .x2m file.
  20. // make sure input src is 1024(x2mHeaderSize) bytes long.
  21. func decryptX2MHeader(src []byte) []byte {
  22. dst := make([]byte, len(src))
  23. for dstIdx := range src {
  24. srcIdx := x2mScrambleTable[dstIdx]
  25. dst[dstIdx] = src[srcIdx] ^ x2mKey[dstIdx%len(x2mKey)]
  26. }
  27. return dst
  28. }