Einzelnen Beitrag anzeigen

Schokohase
(Gast)

n/a Beiträge
 
#8

AW: HASH ermitteln - Python nach Delphi, aber wie ?

  Alt 13. Aug 2019, 11:17
Mal ein Versuch:
Delphi-Quellcode:
uses
  System.Classes,
  System.Hash,
  System.SysUtils;

type
  TChannel = record
    VideoId: Integer;
    AccessToken: string;
    ClientLocation: string;
  end;

function HexDigest(const ABuffer: TBytes): string;
const
  B2H: array [0 .. 15] of Char = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
var
  I, len: Integer;
  str: string;
begin
  str := '';
  len := Length(ABuffer);
  SetLength(str, len * 2);
  for I := 0 to len - 1 do
  begin
    str[I * 2 + Low(str)] := B2H[(ABuffer[I] shr 4) and $0F];
    str[I * 2 + 1 + Low(str)] := B2H[ABuffer[I] and $0F];
  end;
  Result := str;
end;

function BuildHashStr(const SaltStr: string; const AChannel: TChannel; const AServerToken : string): string;
const
  someString = 'dash:widevine';
var
  strToHash: string;
  bufferToHash, binaryHash: TBytes;
  hasher: THashSHA1;
begin
  strToHash := string.Join('', [ //
    AChannel.VideoId.ToString(), //
    SaltStr, //
    AChannel.AccessToken, //
    AServerToken, //
    AChannel.ClientLocation, //
    someString]);
  bufferToHash := TEncoding.UTF8.GetBytes(strToHash);

  hasher := THashSHA1.Create();
  try
    hasher.Update(bufferToHash);
    binaryHash := hasher.HashAsBytes();
  finally
    hasher.Reset;
  end;
  Result := SaltStr.Substring(0, 2) + HexDigest(binaryHash);
end;

procedure Test;
const
  salt = '01!8d8F_)r9]4s[qeuXfP%';
  server_token = 'server_token';
var
  channel: TChannel;
  hashStr: string;
begin
  channel.VideoId := 1;
  channel.AccessToken := 'access_token';
  channel.ClientLocation := 'client_location';

  hashStr := BuildHashStr(salt, channel, server_token);
  Writeln(hashStr);
end;
ergibt
Code:
01ea8c57f1334dfeabb62030212f997f5c4f0f93c1
und
Code:
import hashlib

salt = '01!8d8F_)r9]4s[qeuXfP%'
server_token = 'server_token'
channel = {'video_id':1,'access_token':'access_token','client_location':'client_location'}

client_token = salt[:2] + hashlib.sha1(''.join([str(channel['video_id']), salt, channel['access_token'], server_token, channel['client_location'], 'dash:widevine'] ).encode('utf-8')).hexdigest()

print(client_token)
ergibt das gleiche Ergebnis ... also müsste das passen

Geändert von Schokohase (13. Aug 2019 um 12:38 Uhr)
  Mit Zitat antworten Zitat