GoLang OAuthProvider::generateToken

is this article helpful? yes | no
GoLang replacement for PHP's OAuthProvider::generateToken [Golang Play | edit | history]
import (
	"crypto/sha512"
	"encoding/base64"
	"strings"
)

func hashMiaoSpeed(token, request string) string {
	buildTokens := append([]string{token}, strings.Split(strings.TrimSpace(BUILDTOKEN), "|")...)

	hasher := sha512.New()
	hasher.Write([]byte(request))

	for _, t := range buildTokens {
		if t == "" {
			// unsafe, please make sure not to let token segment be empty
			t = "SOME_TOKEN"
		}

		hasher.Write(hasher.Sum([]byte(t)))
	}

	return base64.URLEncoding.EncodeToString(hasher.Sum(nil))
}

PHP OAuthProvider::generateToken

PHP original manual for OAuthProvider::generateToken [ show | php.net ]

OAuthProvider::generateToken

(PECL OAuth >= 1.0.0)

OAuthProvider::generateTokenGenerate a random token

Description

final public static string OAuthProvider::generateToken ( int $size [, bool $strong = FALSE ] )

Generates a string of pseudo-random bytes.

Parameters

size

The desired token length, in terms of bytes.

strong

Setting to TRUE means /dev/random will be used for entropy, as otherwise the non-blocking /dev/urandom is used. This parameter is ignored on Windows.

Return Values

The generated token, as a string of bytes.

Errors/Exceptions

If the strong parameter is TRUE, then an E_WARNING level error will be emitted when the fallback rand() implementation is used to fill the remaining random bytes (e.g., when not enough random data was found, initially).

Examples

Example #1 OAuthProvider::generateToken() example

<?php
$p 
= new OAuthProvider();

$t $p->generateToken(4);

echo 
strlen($t),  PHP_EOL;
echo 
bin2hex($t), PHP_EOL;

?>

The above example will output something similar to:

4
b6a82c27

Notes

Note:

When not enough random data is available to the system, this function will fill the remaining random bytes using the internal PHP rand() implementation.

See Also